理解数组排序行为php

Dar*_*ric 5 php arrays sorting

我有这个数组

$array = array(2, 1, "img1", "img2", "img10", 1.5, "3.14", "2.72");
Run Code Online (Sandbox Code Playgroud)

应用sort功能后,它变成了

sort($array);

Array
(
    [0] => 2.72
    [1] => 3.14
    [2] => img1
    [3] => img10
    [4] => img2
    [5] => 1
    [6] => 1.5
    [7] => 2
)
Run Code Online (Sandbox Code Playgroud)

我不明白如何进行排序.

任何人都可以解释如何实现输出数组?

编辑:

这里的问题不是SORT FLAG我应该使用哪个,而是问题是如何执行上述排序.任何使用他人的建议在SORT FLAG这里都没用.

Hug*_*ota 6

前两个数字是实际的字符串(你在引号内传递它们),所以它们被这样对待:数字在字母排序中的任何字母之前.

其他的是实数(没有引号),所以函数将它们分开,用数字排序.

array(8)
    0 => '2.72'    [alphabetical]
    1 => '3.14'    [alphabetical]
    2 => 'img1'    [alphabetical]
    3 => 'img10'   [alphabetical]
    4 => 'img2'    [alphabetical]
    5 => 1         [numeric]
    6 => 1.5       [numeric]
    7 => 2         [numeric]
Run Code Online (Sandbox Code Playgroud)

因此,函数必须决定它是否按字母顺序(a,b,c ...)或数字(1,2,3 ..)排序元素,因此它只检查变量类型.

就像Pekka指出的那样,有一些标志可以设置为强制排序类型.它们在文档中描述.


编辑

这个理论在评论中被证明是错误的,我现在完全迷失了(:

这篇文章的评论包含了一些关于这个问题的有趣观点.

  • 让我告诉你,你错了,看看@coanda提供的链接.如果你有机会排序这个数组:("a","b","4",5,4,"true","TRUE",true,false,"c","d").然后对这个数组进行排序:("a","b","4",5,4,"true","TRUE",true,false,"c").通过删除"d"元素,它们返回2个完全不同的结果.根据PHP,这不是一个错误,而是类型杂耍的结果. (2认同)

Sco*_*ott 3

使用 Coanda 链接中的信息作为起点,很明显 PHP 在比较不同类型的对象时使用类型杂技。问题是理解比较过程中什么被转换成什么,我仍然无法找到完整的列表,但总的来说:

string 与 int 比较会变成

(int) string <,>,=, (int) int 
Run Code Online (Sandbox Code Playgroud)

在这种情况下,字符串在转换后变为 0,因此所有整数都大于字符串。(预期的)。对于此类情况,这是我们唯一需要担心的情​​况。

PHP 使用快速排序,并且很可能选择其枢轴点,其中array[n/2]n 是数组中的元素数。知道了这两条信息,我们对上面的数组进行快速排序:

$pivot = $array[n/2] //n is the number of elements, this sets $pivot='img2'
//compare each element in the list (i am going to this by hand for demonstration)

(int) 'img2' < 2 //int to int comparison;'img2' resolves to 0 and 0 < 2
(int) 'img2' < 1 //int to int comparison;'img2' resolves to 0 and 0 < 1
      'img2' > 'img1' // string to string comparison; strcmp is +256 
      'img2' > 'img 10' //string to string comparison; strcmp is +256 
(float) 'img2' < 1.5 //float to float comparison;'img2' resolves to 0 and 0<1.5 
      'img2' > '3.14' //string to string comparison; strcmp is +54
      'img2' > '2.72' //string to string comparison; strcmp is +55
Run Code Online (Sandbox Code Playgroud)

我们现在有两个新数组(一个用于大于,一个用于小于)。

 $greater = array('img1', 'img10', '3.14', '2.72);
 $less = array(2, 1, 1.5);
Run Code Online (Sandbox Code Playgroud)

现在几乎没有必要深入细节,因为我们不小心创建了 2 个数组,其中包含所有易于比较的对象。$greater只有字符串,我们可以假设排序在这里可以正常工作并考虑所有字符串。

sort($greater);
var_dump($greater);
Run Code Online (Sandbox Code Playgroud)

产生

array(4) {
  [0]=>
  string(5) "2.72"
  [1]=>
  string(4) "3.14"
  [2]=>
  string(4) "img1"
  [3]=>
  string(5) "img10"
}
Run Code Online (Sandbox Code Playgroud)

这是我们所期望的,也是上面的结果。我们也做同样的事情$lesser

$lesser = array(2, 1, 1.5);
sort($lesser);
var_dump($lesser);
Run Code Online (Sandbox Code Playgroud)

我们得到

array(3) {
  [0]=>
  int(1)
  [1]=>
  float(1.5)
  [2]=>
  int(2)
}
Run Code Online (Sandbox Code Playgroud)

这也是预料之中的。现在,当我们将所有三个数组连接在一起时(为了递归性,我将“img2”称为数组)。我们得到上面的结果。

Array
(
[0] => 2.72
[1] => 3.14
[2] => img1
[3] => img10
[4] => img2
[5] => 1
[6] => 1.5
[7] => 2
)
Run Code Online (Sandbox Code Playgroud)

$arr[3]为了证明这一点,您可以对同一个数组执行相同的过程,但用整数切换。

$arr = array("img2", 1, "img1", 2, "img10", 1.5, "3.14", "2.72");
sort($arr);
var_dump($arr)
Run Code Online (Sandbox Code Playgroud)

给你一个完全不同的结果,因为枢轴从字符串更改为整数,导致浮点字符串计算为浮点数。

array(8) {
[0]=>
string(4) "img1"
[1]=>
string(5) "img10"
[2]=>
string(4) "img2"
[3]=>
int(1)
[4]=>
float(1.5)
[5]=>
int(2)
[6]=>
string(4) "2.72"
[7]=>
string(4) "3.14"
}
Run Code Online (Sandbox Code Playgroud)