对多维数组的键进行递归排序
我很难尝试对多维数组的键进行递归排序。我尝试使用 usort()
,但没有成功。
示例数据:
[
'first_level' => [
'dir_3' => [
'subdir_1' => [
'file_2.mp4' => (object) [
'name' => 'file_2.mp4',
],
'file_1.mp4' => (object) [
'name' => 'file_1.mp4',
],
],
],
'dir_1' => [
'subdir_2' => [
'file_6.mp4' => (object) [
'name' => 'file_6.mp4',
],
'file_9.mp4' => (object) [
'name' => 'file_9.mp4',
],
'file_7.mp4' => (object) [
'name' => 'file_7.mp4',
],
],
'subdir_1' => [
'file_8.mp4' => (object) [
'name' => 'file_8.mp4',
],
],
],
],
]
期望结果:
[
'first_level' => [
'dir_1' => [
'subdir_1' => [
'file_8.mp4' => (object) [
'name' => 'file_8.mp4',
],
],
'subdir_2' => [
'file_6.mp4' => (object) [
'name' => 'file_6.mp4',
],
'file_7.mp4' => (object) [
'name' => 'file_7.mp4',
],
'file_9.mp4' => (object) [
'name' => 'file_9.mp4',
],
],
],
'dir_3' => [
'subdir_1' => [
'file_1.mp4' => (object) [
'name' => 'file_1.mp4',
],
'file_2.mp4' => (object) [
'name' => 'file_2.mp4',
],
],
],
],
]
I am having a hard time trying recursively sort a multidimensional array on its keys. I tried with usort()
, but with no success.
Sample data:
[
'first_level' => [
'dir_3' => [
'subdir_1' => [
'file_2.mp4' => (object) [
'name' => 'file_2.mp4',
],
'file_1.mp4' => (object) [
'name' => 'file_1.mp4',
],
],
],
'dir_1' => [
'subdir_2' => [
'file_6.mp4' => (object) [
'name' => 'file_6.mp4',
],
'file_9.mp4' => (object) [
'name' => 'file_9.mp4',
],
'file_7.mp4' => (object) [
'name' => 'file_7.mp4',
],
],
'subdir_1' => [
'file_8.mp4' => (object) [
'name' => 'file_8.mp4',
],
],
],
],
]
Desired result:
[
'first_level' => [
'dir_1' => [
'subdir_1' => [
'file_8.mp4' => (object) [
'name' => 'file_8.mp4',
],
],
'subdir_2' => [
'file_6.mp4' => (object) [
'name' => 'file_6.mp4',
],
'file_7.mp4' => (object) [
'name' => 'file_7.mp4',
],
'file_9.mp4' => (object) [
'name' => 'file_9.mp4',
],
],
],
'dir_3' => [
'subdir_1' => [
'file_1.mp4' => (object) [
'name' => 'file_1.mp4',
],
'file_2.mp4' => (object) [
'name' => 'file_2.mp4',
],
],
],
],
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用递归函数在当前级别和所有更深的子数组上调用 ksort。
演示:https://3v4l.org/Xede5
Use a recursive function to call ksort on the current level and all deeper subarrays.
Demo: https://3v4l.org/Xede5
您需要使用 ksort 进行递归。 演示
You need to use ksort with recursion. Demo
在递归函数中没有必要
返回 ksort()
——无论如何,这都会从ksort()
返回不需要的成功布尔值。请注意,当给定非数组时,此函数不会抛出“警告:ksort() 期望参数 1 为数组”——这符合我的要求,但可能不符合您的要求。演示:https://3v4l.org/bogAU
It is not necessary within the recursive function to
return ksort()
-- this would return the unwanted success boolean value fromksort()
anyhow.Note that this function does not throw "Warning: ksort() expects parameter 1 to be array" when given a non-array - this matches my requirements but perhaps not yours. Demo: https://3v4l.org/bogAU
可以公平地假设您希望数据“自然”排序——这意味着目录和文件名的数字部分应该按数字排序,而不是简单的字符串排序。如果不自然排序,
dir_10
会移到dir_2
前面,因为比较两个字符串的第 5 个字符时,1 小于 2。代码:(演示)
SORT_NATURAL
标志应用于ksort()
打电话。nat_ksort_r()
的地方。上面的函数还可以使用经典循环来代替函数迭代器。 (演示)
您甚至可以以完全匿名的方式编写代码。 演示
It is fair to assume that you'd like your data ti be sorted "naturally" -- meaning that the number portion of the directory and file names should be sorted numerically instead of as simple strings. Without sorting naturally,
dir_10
will be moved in front ofdir_2
because when comparing the 5th character of the two strings, 1 is less than 2.Code: (Demo)
SORT_NATURAL
flag to theksort()
call.__METHOD__
magic constant. This makes one less place to changenat_ksort_r()
if you wish to call the custom function something else.The above function can also use a classic loop instead of a functional iterator. (Demo)
You can even write the code in a completely anonymous fashion. Demo