我有一个像:
这样的数组Array
(
[0] => Array
(
[lop] => 7A1
[tongdiem] => 4
)
[1] => Array
(
[lop] => 7A2
[tongdiem] => 5
)
[2] => Array
(
[lop] => 7A3
[tongdiem] => 6
)
[3] => Array
(
[lop] => 7A4
[tongdiem] => 30
)
[4] => Array
(
[lop] => 7A5
[tongdiem] => 21
)
)
我需要排序[tongdiem] ASC,但不要改变数组键,这就像:
Array
(
[0] => Array
(
[lop] => 7A1
[tongdiem] => 4
)
[1] => Array
(
[lop] => 7A2
[tongdiem] => 5
)
[2] => Array
(
[lop] => 7A3
[tongdiem] => 6
)
[3] => Array
(
[lop] => 7A5
[tongdiem] => 21
)
[4] => Array
(
[lop] => 7A4
[tongdiem] => 30
)
)
我试过asort,一些php数组内置函数,但没有工作。
我需要使用[tongdiem]值ASC排序数组,但不能更改数组键。
我该怎么办?
答案 0 :(得分:2)
http://php.net/manual/en/function.usort.php
function cmp($a, $b) {
if ($a['tongdiem'] == $b['tongdiem']) {
return 0;
}
return ($a['tongdiem'] < $b['tongdiem']) ? -1 : 1;
}
usort($array, "cmp");