/原始数组是这样的,希望能够按照第二维中的run_date升序或者降序进行排序:
$arr = array(
0 => array(
'run_date' => '2017-11-21',
'count' => '5'
),
1 => array(
'run_date' => '2017-11-20',
'count' => '10'
),
2 => array(
'run_date' => '2017-11-22',
'count' => '10'
)
);
//希望得到:
$arr = array(
0 => array(
'run_date' => '2017-11-20',
'count' => '5'
),
1 => array(
'run_date' => '2017-11-21',
'count' => '10'
),
2 => array(
'run_date' => '2017-11-22',
'count' => '10'
)
);
//先用array_column 多维数组按照纵向(列)取出
$date = array_column($arr, 'run_date');
//上面得到的结果:array(0=>'2017-11-21',1=>'2017-11-20',3=>'2017-11-22')
//再用array_multisort 结合array_column得到的结果对$arr进行排序
array_multisort($date, SORT_ASC, $arr);
//最终的结果:
$arr = array(
0 => array(
'run_date' => '2017-11-20',
'count' => '5'
),
1 => array(
'run_date' => '2017-11-21',
'count' => '10'
),
2 => array(
'run_date' => '2017-11-22',
'count' => '10'
)
);
此篇文章由DurkBlue发布,感谢转载需要请注明来处