我得到了一个这样的数组,带有未分类的外部索引。
$a = array(
(1) => array(1, 2, 3, 0, 5, 4),
(0) => array(2, 1, 5, 0, 3, 4)
);
echo "<br/>Before Sorting: ";
print_r($a);
foreach($a as $b)
array_multisort($b, SORT_ASC, SORT_NUMERIC);
echo "<br/>After Sorting: ";
print_r($a);
给我输出如下
排序前:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => 5
[5] => 4
)
[0] => Array
(
[0] => 2
[1] => 1
[2] => 5
[3] => 0
[4] => 3
[5] => 4
)
)
排序后:
Array
(
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => 5
[5] => 4
)
[0] => Array
(
[0] => 2
[1] => 1
[2] => 5
[3] => 0
[4] => 3
[5] => 4
)
)
我想要的是什么
$a = array(
(0) => array(2, 1, 5, 0, 3, 4),
(1) => array(1, 2, 3, 0, 5, 4)
);
请告诉我如何处理.........
答案 0 :(得分:4)
如何使用ksort(因为您需要按键重新排序数组)?
$a = array(
1 => array(1, 2, 3, 0, 5, 4),
0 => array(2, 1, 5, 0, 3, 4)
);
echo "<br/>Before Sorting: ";
print_r($a);
ksort($a);
echo "<br/>After Sorting: ";
print_r($a);
答案 1 :(得分:1)
由于您只想按顶级索引排序,因此您不需要使用多重排序。你甚至不需要循环。
试试这个:
$a = array(
(1) => array(1, 2, 3, 0, 5, 4),
(0) => array(2, 1, 5, 0, 3, 4)
);
ksort($a);
print_r($a);
应该给你你想要的东西。