Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中根据包含的关联数组对数组进行排序?_Php_Sorting - Fatal编程技术网

如何在php中根据包含的关联数组对数组进行排序?

如何在php中根据包含的关联数组对数组进行排序?,php,sorting,Php,Sorting,我有一个关联数组: a[0] = array("value" => "fred", "score" => 10); a[1] = array("value" => "john", "score" => 50); a[2] = array("value" => "paul", "score" => 5); 我想根据关联数组的“score”值对我的“a”数组进行排序 它将成为: a[0] = array("value" => "paul", "score"

我有一个关联数组:

a[0] = array("value" => "fred", "score" => 10);
a[1] = array("value" => "john", "score" => 50);
a[2] = array("value" => "paul", "score" => 5);
我想根据关联数组的“score”值对我的“a”数组进行排序

它将成为:

a[0] = array("value" => "paul", "score" => 5);
a[1] = array("value" => "fred", "score" => 10);
a[2] = array("value" => "john", "score" => 50);
有人能帮我吗?

您需要使用和比较功能

比如:

function cmp($a, $b)
{
    if ($a['score'] == $b['score']) {
        return 0;
    }
    return ($a['score'] < $b['score']) ? -1 : 1;
}
函数cmp($a,$b)
{
如果($a['score']=$b['score']){
返回0;
}
回报($a['score']<$b['score'])?-1:1;
}