一维数组排序可以使用asort(升序)/ksort(降序),二维数组排序可以使用array_mutisort和usort进行排序
<?php $users = array( array('id'=>1,'age'=>'12'), array('id'=>2,'age'=>'13'), array('id'=>3,'age'=>'44'), array('id'=>4,'age'=>'22'), array('id'=>5,'age'=>'11')); $ages = array(); foreach ($users as $key => $val) { $ages[] = $val['age']; } echo "<pre>"; print_r($ages); //升序排列age,按照age的顺序排列$users array_multisort($ages, SORT_ASC, $users); print_r($users); //使用这个方法,会比较麻烦些,要将age提取出来存储到一维数组里,然后按照age升序排列。 /*执行后,$users就是排序好的数组了,可以打印出来看看。如果需要先按年龄升序排列,再按照ID升序排列,方法同上,就是多提取一个名称数组出来,最后的排序方法这样调用 代码如下:array_multisort($ages, SORT_ASC, $ID, SORT_ASC, $users);*/ /*( [0] => 12 [1] => 13 [2] => 44 [3] => 22 [4] => 11 ) Array ( [0] => Array ( [id] => 5 [age] => 11 ) [1] => Array ( [id] => 1 [age] => 12 ) [2] => Array ( [id] => 2 [age] => 13 ) [3] => Array ( [id] => 4 [age] => 22 ) [4] => Array ( [id] => 3 [age] => 44 ) )*/