如何在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;
}