简体   繁体   English

php array_multisort如何工作?

[英]how php array_multisort work?

i have some problem to understand array_multisort 我有一些问题需要了解array_multisort

See how it sorts when two values are the same: 看两个值相同时的排序方式:

 $a1=array("Dog","Dog","Cat");
 $a2=array("Pluto","Fido","Missy");
 array_multisort($a1,$a2);
 print_r($a1);
 print_r($a2);

The output of the code above will be: 上面代码的输出将是:

 Array ( [0] => Cat [1] => Dog [2] => Dog )
 Array ( [0] => Missy [1] => Fido [2] => Pluto )

let me know why Missy comes first, if you do by ascending it must be Array ( [0] => Fido, [1] => Missy, [2] => Pluto ) for descending vise versa 让我知道为什么Missy是第一个,如果你通过提升它必须是Array([0] => Fido,[1] => Missy,[2] => Pluto)下降反之亦然

also see this 也看到这个

With sorting parameters: 使用排序参数:

$a1=array("Dog","Dog","Cat");
$a2=array("Pluto","Fido","Missy"); 
array_multisort($a1,SORT_ASC,$a2,SORT_DESC); 
print_r($a1); 
print_r($a2);

The output of the code above will be: 上面代码的输出将是:

 Array ( [0] => Cat [1] => Dog [2] => Dog ) 
 Array ( [0] => Missy [1] => Pluto [2] => Fido )

but Array ( [0] => Missy [1] => Pluto [2] => Fido ) not at SORT_DESC is some type of mixed up. 但是数组([0] => Missy [1] => Pluto [2] => Fido)不是在SORT_DESC是某种类型的混合。

can some one explain me how the array_multisort is working, so that i can understand how it's working. 有人可以解释一下array_multisort是如何工作的,这样我就能理解它是如何工作的。

Well, you're sorting the arrays in a similar way to programs like Excel. 好吧,您正在以与Excel等程序类似的方式对数组进行排序。 Each array corresponds to a column. 每个数组对应一列。

First, all arrays are sorted by the first array given. 首先,所有数组都按给定的第一个数组排序。 If there are identical values, those affected are sorted by the second array given. 如果存在相同的值,则受影响的值将按给定的第二个数组排序。 If there are again equal values, the third array is used, etc. 如果值再次相等,则使用第三个数组等。

Or in other words: The arrays are sorted using all arrays, but beginning on the right (if you assume it really sorts by all columns once). 换句话说:数组是使用所有数组排序的,但是从右边开始(如果你假设它真的按所有列排序一次)。

For your particular example (the second one): 对于您的特定示例(第二个):

At first you want to sort in ascending order, so Cat will be first. 首先,您希望按升序排序,因此Cat将成为第一个。 Therefore the last array element will be moved to the first position in both arrays. 因此,最后一个数组元素将移动到两个数组中的第一个位置。 The other two elements, Dog are equal. 另外两个元素, Dog是平等的。 This causes the function to look at the next array. 这会导致函数查看下一个数组。 It's told to sort this array in descending order, so Pluto comes first. 它被告知按降序排列这个数组,所以Pluto是第一个。 In this case this leads to the result that the elements aren't moved at all (as their order is correct already). 在这种情况下,这导致元素根本不移动(因为它们的顺序已经正确)。

第二个数组中的条目对应于第一个数组中的相同条目。

If you look at the documentation and the first example, you'll notice that this is the expected behavior. 如果您查看文档和第一个示例,您会注意到这是预期的行为。

With two arguments, both arrays: the first array is sorted; 有两个参数,两个数组:第一个数组已排序; the second array will have its corresponding values re-arranged and sorted if the corresponding values in first column tie. 如果第一列中的对应值绑定,则第二个数组将重新排列和排序其相应的值。 As for your example, think of it as you're doing a SQL ORDER BY Animal, Name : 至于你的例子,当你正在做一个SQL ORDER BY Animal, Name想一想它ORDER BY Animal, Name

  1. Cat comes first 猫是第一位的
  2. The two Dogs have a tie so Fido comes first because Fido < Pluto 这两只狗有一条领带,所以Fido首先是因为Fido <冥王星

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM