使用 array_multisort 对多维数组排序
array_multisort() 函数对多个数组或多维数组进行排序。
用法详看:http://www.w3school.com.cn/php/func_array_multisort.asp
例子:
把二维数组按name长度赋值给id,并重新排序。
$arr = array( array('id' => 0, 'name' => '123833'), array('id' => 0, 'name' => 'aaa'), array('id' => 0, 'name' => 'albabaababa'), array('id' => 0, 'name' => '12356'), array('id' => 0, 'name' => '123abc') ); foreach($arr as $k=>&$v){ //此处引用赋值才会改变原数组的值 $v['id'] = strlen($v['name']); } array_multisort($arr); //对多维数组排序 echo "<pre>"; print_r($arr); echo "</pre>";
此时数组变为:
Array ( [0] => Array ( [id] => 3 [name] => aaa ) [1] => Array ( [id] => 5 [name] => 12356 ) [2] => Array ( [id] => 6 [name] => 123833 ) [3] => Array ( [id] => 6 [name] => 123abc ) [4] => Array ( [id] => 11 [name] => albabaababa ) )