我有如下的数组
Array
(
[0] => Array
(
[0] => 1280
[id] => 1280
)
[1] => Array
(
[0] => 2261
[id] => 2261
)
[2] => Array
(
[0] => 1280
[id] => 1280
)
)
在php中,我如何根据“id”的值从低到高排序?
答案 0 :(得分:11)
使用usort(),例如:
function sortById($x, $y) {
return $x['id'] - $y['id'];
}
usort($array, 'sortById');
echo "<pre>"; print_r($array);
答案 1 :(得分:0)
使用 array_multisort(),如下所示:
<?php
$multiArray = Array(
Array("id" => 120, "name" => "Defg"),
Array("id" => 62, "name" => "Abcd"),
Array("id" => 99, "name" => "Bcde"),
Array("id" => 2, "name" => "Cdef"));
$tmp = Array();
foreach($multiArray as &$ma)
$tmp[] = &$ma["id"];
array_multisort($tmp, $multiArray);
foreach($multiArray as &$ma)
echo $ma["id"]."<br/>";
/* Outputs
2
62
99
120
*/
?>