我有一个虚拟数组,我想排序。
我怎么能有以下结果
用foreach?With while next()与RecursiveIterator与IteratorIterator
哪一个是最快的?
数组
$files = array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'wp-config.php',
'wp-content' =>
array (
'uploads' =>
array (
2013 =>
array (
'05' =>
array (
0 => 'kabeduvarkad-1024x768.jpg',
1 => 'kabeduvarkad-150x150.jpg',
2 => 'kabeduvarkad-300x225.jpg',
3 => 'kabeduvarkad-940x198.jpg',
),
10 =>
array (
),
),
2014 =>
array (
'02' =>
array (
),
),
2015 => 'de.php',
),
),
'wp-update' =>
array (
0 => 'wp-update.tar',
1 => 'wp-update.tar.gz',
2 => 'wp-update1.tar',
3 => 'wp-update1.tar.gz',
),
4 => 'wp-update.tar.gz',
);
预期的结果
$expected = array (
0 => 'do-update.php',
1 => 'sitemap.xml',
2 => 'sitemap.xml.gz',
3 => 'test.php',
4 => 'wp-config.php',
5 => 'wp-content/',
6 => 'wp-content/uploads/',
7 => 'wp-content/uploads/2013/',
8 => 'wp-content/uploads/2013/05/',
9 => 'wp-content/uploads/2013/05/kabeduvarkad-1024x768.jpg',
10 => 'wp-content/uploads/2013/05/kabeduvarkad-150x150.jpg',
11 => 'wp-content/uploads/2013/05/kabeduvarkad-300x225.jpg',
12 => 'wp-content/uploads/2013/05/kabeduvarkad-940x198.jpg',
13 => 'wp-content/uploads/2013/05/kabeduvarkad.jpg',
14 => '...'
);
array_walk_recursive
就是你要找的。
示例(未测试):
$expected = [];
array_walk_recursive($your_array, function($item, $key) {
// push item on to $expected
$expected[] = $item;
});
工作示例