本文介绍了在 PHP 中的每个级别递归排序多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这种形式的数组:
I have an array of this form:
Array
(
[first_level] => Array
(
[dir_3] => Array
(
[subdir_1] => Array
(
[file_2.mp4] => stdClass Object
(
[name] => file_2.mp4
)
[file_1.mp4] => stdClass Object
(
[name] => file_1.mp4
)
)
)
[dir_1] => Array
(
[subdir_2] => Array
(
[file_6.mp4] => stdClass Object
(
[name] => file_6.mp4
)
[file_9.mp4] => stdClass Object
(
[name] => file_9.mp4
)
[file_7.mp4] => stdClass Object
(
[name] => file_7.mp4
)
)
[subdir_1] => Array
(
[file_8] => stdClass Object
(
[name] => file_8.mp4
)
)
)
)
)
我需要这样订购:
Array
(
[first_level] => Array
(
[dir_1] => Array
(
[subdir_1] => Array
(
[file_8] => stdClass Object
(
[name] => file_8.mp4
)
)
[subdir_2] => Array
(
[file_6.mp4] => stdClass Object
(
[name] => file_6.mp4
)
[file_7.mp4] => stdClass Object
(
[name] => file_7.mp4
)
[file_9.mp4] => stdClass Object
(
[name] => file_9.mp4
)
)
)
[dir_3] => Array
(
[subdir_1] => Array
(
[file_1.mp4] => stdClass Object
(
[name] => file_1.mp4
)
[file_2.mp4] => stdClass Object
(
[name] => file_2.mp4
)
)
)
)
)
我浏览了其他类似的问题,我一直在尝试使用 usor 但我无法理解它.:S
I browsed through other similar questions, and I've been trying to solve it with usort but I couldn't get my head around it. :S
有什么想法吗?
推荐答案
使用递归函数:
// Note this method returns a boolean and not the array
function recur_ksort(&$array) {
foreach ($array as &$value) {
if (is_array($value)) recur_ksort($value);
}
return ksort($array);
}
这篇关于在 PHP 中的每个级别递归排序多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!