我有一个数组,我想按优先级DESC的值对其进行排序。
"status":"OK",
"baseurl":"http://www.test.com",
"pictureurl":"http://www.test.com",
"result":[
{
"videoid":"60",
"username":"1556495708",
"submittime":"1400112000",
"videotitle":"Test Video",
"videodescription":"Test Video Description",
"priority":12
},
{
"videoid":"61",
"username":"1556495708",
"submittime":"1400151306",
"videotitle":"Test Video",
"videodescription":"Test Video Description",
"priority":20
}
]
我使用此代码但它不起作用。它总是给出result =“null”
我的二手代码
function aasort (&$array, $key) {
$sorter=array();
$ret=array();
reset($array);
foreach ($array as $ii => $va) {
$sorter[$ii]=$va[$key];
}
asort($sorter);
foreach ($sorter as $ii => $va) {
$ret[$ii]=$array[$ii];
}
$array=$ret;
}
$output = array(
'status' => $status,
'baseurl' =>$baseURL,
'pictureurl' =>$imagebaseURL,
'result' => aasort($info,"priority")
);
如何按“优先级”DESC
的值对此数组进行排序答案 0 :(得分:1)
这样做,你需要提取数组然后先解码它。
echo '<pre>';
$json = '
[
{
"videoid":"60",
"username":"1556495708",
"submittime":"1400112000",
"videotitle":"Test Video",
"videodescription":"Test Video Description",
"priority":12
},
{
"videoid":"61",
"username":"1556495708",
"submittime":"1400151306",
"videotitle":"Test Video",
"videodescription":"Test Video Description",
"priority":20
}
]';
$array = json_decode($json, true);
function do_sort($a, $b) {
return $b['priority'] - $a['priority'];
}
usort($array, 'do_sort');
print_r($array);
答案 1 :(得分:0)
PHP&gt; = 5.5.0 :
$array = json_decode($json, true);
array_multisort(array_column($array['result'], 'priority'),
SORT_DESC, $array['result']);