如何按
nums
值对该数组中的两行进行排序?
[
'nums' => [34, 12, 13],
'players' => ['Mike', 'Bob', 'Mary']
]
想要的结果:
[
'nums' => [12, 13, 34],
'players' => ['Bob', 'Mary', 'Mike']
]
array_multisort($x['nums'],$x['players']);
sort function.
bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
该函数对数组进行排序。此功能完成后,元素将从最低到最高排列。
asort
和 arsort
编辑
我没有考虑你的多维数组。
<?php
//code derived from comments on the php.net/sort page.
// $sort used as variable function--can be natcasesort, for example
function sort2d( &$arrIn, $index = null, $sort = 'sort') {
// pseudo-secure--never allow user input into $sort
if (strpos($sort, 'sort') === false) {$sort = 'sort';}
$arrTemp = Array();
$arrOut = Array();
foreach ( $arrIn as $key=>$value ) {
reset($value);
$arrTemp[$key] = is_null($index) ? current($value) : $value[$index];
}
$sort($arrTemp);
foreach ( $arrTemp as $key=>$value ) {
$arrOut[$key] = $arrIn[$key];
}
$arrIn = $arrOut;
}
?>