参见英文答案 >
array_multisort with maintaining numeric index association 2个
如何对多维数组进行深度排序并保留其键?
如何对多维数组进行深度排序并保留其键?
$array = [ '2' => [ 'title' => 'Flower','order' => 3 ],'3' => [ 'title' => 'Rock','order' => 1 ],'4' => [ 'title' => 'Grass','order' => 2 ] ]; foreach ($array as $key => $row) { $items[$key] = $row['order']; } array_multisort($items,SORT_DESC,$array); print_r($array);
结果:
Array ( [0] => Array ( [title] => Flower [order] => 3 ) [1] => Array ( [title] => Grass [order] => 2 ) [2] => Array ( [title] => Rock [order] => 1 ) )
我所追求的是:
Array ( [2] => Array ( [title] => Flower [order] => 3 ) [4] => Array ( [title] => Grass [order] => 2 ) [3] => Array ( [title] => Rock [order] => 1 ) )
有任何想法吗?
也许这就是你在找什么:
Online Example
原文链接:https://www.f2er.com/php/130329.html保留数组键,使用order列排序并再次组合它们.
$keys = array_keys($array); array_multisort( array_column($array,'order'),SORT_NUMERIC,$array,$keys ); $array = array_combine($keys,$array); echo '<pre>'; print_r($array);