我有两个数组需要对A-> Z进行排序,但是需要保留所有数字索引。我不知道如何处理这个问题。
注意:在$complex
数组中,只要保留相关键并且子数组内容已排序,子数组的顺序无关紧要。
两个示例中都必须保留所有密钥。
<?php
$simple = array(
20 => 'Hello',
10 => 'Cat',
30 => 'Dog'
);
$complex = array(
30 => array(
5 => 'foo',
10 => 'bar'
),
10 => array(
4 => 'a',
1 => 'b'
),
20 => array()
);
// simple
array(
10 => Cat,
30 => Dog,
20 => Hello
)
// complex; order of top-level indexes (30, 10, and 20) is not important but the key *must* be preserved
array(
30 => array(
10 => bar,
5 => foo,
),
10 => array(
4 => a,
1 => b,
),
20 => array()
)
答案 0 :(得分:5)
$simple = array(
20 => 'Hello',
10 => 'Cat',
30 => 'Dog'
);
asort($simple);
$complex = array(
30 => array(
5 => 'foo',
10 => 'bar'
),
10 => array(
4 => 'a',
1 => 'b'
),
20 => array()
);
array_walk($complex, 'asort');
print_r($complex);
答案 1 :(得分:0)
我相信这就是你要找的,PHP'asort'方法: