我有一个脚本可以从数据库获取一些值。
变量的结构如下:
$dump["likes"] = 1234;
$likes["data"][$i]["name"] = "ABCDEFG";
for ($i=0;$i<=$max;$i++) {
$data[$i]["likes"] = $dump["likes"];
$data[$i]["name"] = $likes["data"][$i]["name"];
}
//Print sorted array here (highest value in "like" first)
我只需要一种方法来找出哪个条目中最大的“喜欢”是:)
我已经尝试过
array_multisort()
,但它显示我“尺寸不一致”或一些错误......
编辑:
当我在结束循环之外打印($data)时,它看起来像这样:
Array (
[0] => Array ( [likes] => 3485109 [name] => Google )
[1] => Array ( [likes] => 78535 [name] => Youtube )
[2] => Array ( [likes] => 2433041 [name] => Bing )
)
我想要一个像这样的数组:
Array (
[0] => Array ( [likes] => 3485109 [name] => Google )
[1] => Array ( [likes] => 2433041 [name] => Bing )
[2] => Array ( [likes] => 78535 [name] => Youtube )
)
//Sorted by "likes"
这应该可以解决问题
function cmp($a, $b){
if ($a['likes']>$b['likes'])
return 1;
else if ($a['likes']<$b['likes'])
return -1;
else return 0;
}
usort($data, 'cmp');