Home > Article > Backend Development > PHP two-dimensional array sorting code_PHP tutorial
A simple php two-dimensional array is sorted by the elements in the second dimension. This does not affect the data. It only sorts the contents of the array. Friends in need can refer to it.
代码如下 | 复制代码 |
$ar = array( 0 => array('a'=>5, 'b'=>3, 'c'=>3), 1 => array('a'=>5, 'b'=>2, 'c'=>1), 2 => array('a'=>1, 'b'=>1, 'c'=>2) ); $a = array(); $b = array(); foreach($ar as $k=>$v) { $a[$k] = $v['a']; //$b[$k] = $v['b']; //$c[$k] = $v['c']; } array_multisort($a, SORT_ASC, $ar); //array_multisort($a, SORT_DESC, $b, SORT_ASC, $ar); print_r($ar); |