在 php 中 usort() 函数是如何工作的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8456029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:46:04  来源:igfitidea点击:

In php how does usort() function works

phpsortingmultidimensional-arrayusort

提问by Sarmen B.

I have looked at the php documentation, tutorials online and none of them how usort is actually working. I have an example i was playing with below.

我已经查看了 php 文档、在线教程,但没有一个了解 usort 的实际工作方式。我有一个我在下面玩的例子。

$data = array(

    array('msg' => 'some text','month' => 11,'level' => 10),

    array('msg' => 'some text','month' => 5,'level' => 10),

    array('msg' => 'some text','month' => 8,'level' => 10),

    array('msg' => 'some text','month' => 12,'level' => 10),

    array('msg' => 'some text','month' => 2,'level' => 10),

    array('msg' => 'some text','month' => 3,'level' => 10),

    array('msg' => 'some text','month' => 4,'level' => 10),

    array('msg' => 'some text','month' => 7,'level' => 10),

    array('msg' => 'some text','month' => 10,'level' => 10),

    array('msg' => 'some text','month' => 1,'level' => 10),

    array('msg' => 'some text','month' => 6,'level' => 10),

    array('msg' => 'some text','month' => 9,'level' => 10)

);

I wanted to be able to sort the months from 12 to 1 (since their unorganized) through some help this was the solution

我希望能够通过一些帮助将月份从 12 排序到 1(因为它们是无组织的)这是解决方案

function cmp($a, $b)
{
    if ($a["month"] == $b["month"]) 
    {
       return 0;
    }
    return ($a["month"] < $b["month"]) ? -1 : 1;
}

usort($data, "cmp");

but i dont understand how the function cmp sorts the array. i tried printing out each variable $a and $b like this:

但我不明白函数 cmp 如何对数组进行排序。我试着像这样打印出每个变量 $a 和 $b:

function cmp($a, $b)
{
   echo "a: ".$a['month']."<br/>";
   echo " b: ".$b['month']."<br/>";
   echo "<br/><br/>";
}

and the output was

输出是

a: 3
b: 5

a: 9
b: 3

a: 3
b: 8

a: 6
b: 3

a: 3
b: 12

a: 1
b: 3

a: 3
b: 2

a: 10
b: 3

a: 3
b: 11

a: 7
b: 3

a: 4
b: 3

a: 12
b: 2

a: 5
b: 12

a: 12
b: 11

a: 8
b: 12

a: 5
b: 8

a: 2
b: 11

a: 6
b: 9

a: 7
b: 6

a: 6
b: 4

a: 10
b: 6

a: 1
b: 6

a: 9
b: 4

a: 7
b: 1

a: 10
b: 7

it makes no sense to how the sort is working and why cmp($a, $b) is used. i have tried to print out all its processes as you can see but have not come to any solution to how it all works..

排序如何工作以及为什么使用 cmp($a, $b) 是没有意义的。正如你所看到的,我试图打印出它的所有进程,但还没有找到任何关于它如何工作的解决方案。

thanks

谢谢

回答by halfdan

The function cmpitself doesn't do the sorting. It just tells usortif a value is smaller, equal or greater than another value. E.g. if $a = 5and $b = 9it will return 1 to indicate that the value in $bis greater than the one in $a.

函数cmp本身不进行排序。它只是告诉usort一个值是否小于、等于或大于另一个值。例如 if $a = 5and$b = 9它将返回 1 表示 in 的值$b大于in 的值$a

Sorting is done by usort.

排序由usort.

回答by knittl

The callback provided to the sorting functions in PHP have three return values:

PHP 中提供给排序函数的回调有三个返回值:

0:  both elements are the same
-1 (<0): the first element is smaller than the second
1 (>0):  the first element is greater

Now, usortprobably uses some kind of quicksort or mergesort internally. For each comparison it calls your callback with two elements and then decides if it needs to swap them or not.

现在,usort可能在内部使用某种快速排序或归并排序。对于每次比较,它会使用两个元素调用您的回调,然后决定是否需要交换它们。

回答by Paul

usort()uses an implementation of Quicksortto sort the array, it calls your cmpfunction as many times as it needs to to fully sort the array using that algorithm.

usort()使用Quicksort的实现来对数组进行排序,它会根据需要调用您的cmp函数以使用该算法对数组进行完全排序。

回答by pat

As the others mentioned, usort uses the Quicksort algorithm. On a side note, you do not need to explicitly do the comparision between two strings. You can use PHP's string compare methods.

正如其他人提到的,usort 使用 Quicksort 算法。附带说明一下,您不需要明确地在两个字符串之间进行比较。您可以使用 PHP 的字符串比较方法。

The function that you created,

您创建的函数,

function cmp($a, $b)
{
    if ($a["month"] == $b["month"]) 
    {
       return 0;
    }
    return ($a["month"] < $b["month"]) ? -1 : 1;
}

could simply be written as follows

可以简单地写成如下

function compareMyStrings($a, $b){
    return strnatcmp($a["month"], $b["month"]);
}

Hope this helps!

希望这可以帮助!

回答by Lead Developer

This is another solution I found

这是我发现的另一个解决方案

<?php 
$users = array( array( "peter", "male", "46"), 
                array( "hans", "male", "19"), 
                array( "john", "male", "30"), 
                array( "linda", "female", "54"), 
                array( "erika", "female", "79")); 
usort($users, "whatevername"); 
function whatevername($whatever1, $whatever2) 
{ 
    // $whatever1 and $whatever2 are items from the $user array. 
    // index [2] is the age. 
    // Check if $whatever1 is older than $whatever2. 
    // Return 1 tells usort to swap the positions. 
    return $whatever1[2] > $whatever2[2]; 
} 

echo("<pre>"); 
print_r($users); 
echo("</pre>"); 
?>