可能重复:
How to sort an array based on a specific field in the array?
sorting array based on inner-array key-value
如何按内部数组值排序数组?
其中一个案例 - 需要按数字排序(80,25,85等): 输入:
array (
0 =>
array (
'item1' => 80,
),
1 =>
array (
'item2' => 25,
),
2 =>
array (
'item3' => 85,
),
)
输出:
array (
0 =>
array (
'item2' => 25,
),
1 =>
array (
'item1' => 80,
),
2 =>
array (
'item3' => 85,
),
)
提前致谢
答案 0 :(得分:0)
您需要使用usort
$array = array (
0 =>
array (
'item1' => 80,
),
1 =>
array (
'item2' => 25,
),
2 =>
array (
'item3' => 85,
),
);
function my_sort_cmp($a, $b) {
reset($a);
reset($b);
return current($a) < current($b) ? -1 : 1;
}
usort($array, 'my_sort_cmp');
print_r($array);
输出:
(
[0] => Array
(
[item2] => 25
)
[1] => Array
(
[item1] => 80
)
[2] => Array
(
[item3] => 85
)
)
答案 1 :(得分:0)
您需要使用usort,这是一个通过用户定义的函数对数组进行排序的函数。类似的东西:
function cmp($a, $b)
{
$a = reset($a); // get the first array elements
$b = reset($b); // for comparison.
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
usort($yourArray,"cmp")