php array_multisort如何工作? [英] how php array_multisort work?

查看:84
本文介绍了php array_multisort如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解array_multisort时遇到了一些问题

i have some problem to understand 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);

以上代码的输出为:

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

让我知道为什么Missy首先出现,如果您通过提升来做到的话 数组([0] => Fido,[1] => Missy,[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

另请参阅

具有排序参数:

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

以上代码的输出为:

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

而不是SORT_DESC处的数组([0] =>小姐[1] =>冥王星[2] => Fido)是某种混淆.

but Array ( [0] => Missy [1] => Pluto [2] => Fido ) not at SORT_DESC is some type of mixed up.

有人可以解释一下array_multisort是如何工作的,以便我能理解它是如何工作的.

can some one explain me how the array_multisort is working, so that i can understand how it's working.

推荐答案

好吧,您正在以类似于Excel之类的程序对数组进行排序.每个数组对应一个列.

Well, you're sorting the arrays in a similar way to programs like 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):

首先,您要按升序排序,因此Cat将是第一个.因此,最后一个数组元素将被移动到两个数组中的第一个位置.其他两个元素Dog相等.这会使函数查看下一个数组.它被告知要按降序对数组进行排序,因此Pluto首先出现.在这种情况下,这导致元素根本不移动(因为它们的顺序已经正确).

At first you want to sort in ascending order, so Cat will be first. Therefore the last array element will be moved to the first position in both arrays. The other two elements, Dog are equal. This causes the function to look at the next array. It's told to sort this array in descending order, so Pluto comes first. In this case this leads to the result that the elements aren't moved at all (as their order is correct already).

这篇关于php array_multisort如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆