这里我有一个多维数组,如下所示:
Array
(
[1] => Array
(
[0] => 100
[1] => a
)
[2] => Array
(
[0] => 1000
[1] => b
)
[3] => Array
(
[0] => 50
[1] => c
)
[4] => Array
(
[0] => 500
[1] => d
)
[5] => Array
(
[0] => 1500
[1] => e
)
)
我想做的就是根据[0]值对数组进行排序,所以它看起来像这样:
Array
(
[1] => Array
(
[0] => 1500
[1] => e
)
[2] => Array
(
[0] => 1000
[1] => b
)
[3] => Array
(
[0] => 500
[1] => d
)
[4] => Array
(
[0] => 100
[1] => a
)
[5] => Array
(
[0] => 50
[1] => c
)
)
你有什么建议我想做什么吗?提前致谢
答案 0 :(得分:1)
最简单的方法是使用用户定义的函数进行排序:
<?php
$values = [
1 => [100, 'a'],
2 => [1000, 'b'],
3 => [50, 'c'],
4 => [500, 'd'],
5 => [1500, 'e']
];
usort($values, function($a, $b){
return $a[0] < $b[0];
});
var_dump($values);
输出显然是:
array(5) {
[0] =>
array(2) {
[0] =>
int(1500)
[1] =>
string(1) "e"
}
[1] =>
array(2) {
[0] =>
int(1000)
[1] =>
string(1) "b"
}
[2] =>
array(2) {
[0] =>
int(500)
[1] =>
string(1) "d"
}
[3] =>
array(2) {
[0] =>
int(100)
[1] =>
string(1) "a"
}
[4] =>
array(2) {
[0] =>
int(50)
[1] =>
string(1) "c"
}
}