【发布时间】:2018-05-03 09:27:13
【问题描述】:
我想将以下多维数组转换为单个数组。 仅子数组的parent_id 列应该与父数组的category_id 相同。而子数组的sort_order_number 应该等于它们在children 数组中的位置。并且父数组的sort_order_number 应该等于它们在 parent 数组中的位置:
当前多维数组:
array:2 [▼
0 => array:12 [▼
"text_az" => "title1"
"text_en" => "title1.1"
"text_ru" => "title1.2"
"slug" => "sfsf"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 1
"sort_order_number" => 1
"parent_id" => null
"created_at" => null
"updated_at" => null
"children" => array:1 [▼
0 => array:12 [▼
"text_az" => "title3"
"text_en" => "title3.1"
"text_ru" => "title3.2"
"slug" => "sdf"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 3
"sort_order_number" => 1
"parent_id" => null
"created_at" => null
"updated_at" => null
"children" => array:1 [▼
0 => array:11 [▼
"text_az" => "title2"
"text_en" => "title2.1"
"text_ru" => "title2.2"
"slug" => "fsdfsf"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 2
"sort_order_number" => 2
"parent_id" => null
"created_at" => null
"updated_at" => null
]
]
]
]
]
1 => array:11 [▼
"text_az" => "title4"
"text_en" => "title4.1"
"text_ru" => "title4.2"
"slug" => "treteterter"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 4
"sort_order_number" => 2
"parent_id" => null
"created_at" => null
"updated_at" => null
]
]
转换后我希望拥有的一维数组:
array:3 [▼
0 => array:12 [▼
"text_az" => "title1"
"text_en" => "title1.1"
"text_ru" => "title1.2"
"slug" => "sfsf"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 1
"sort_order_number" => 1
"parent_id" => null
"created_at" => null
"updated_at" => null
]
1 => array:12 [▼
"text_az" => "title3"
"text_en" => "title3.1"
"text_ru" => "title3.2"
"slug" => "sdf"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 3
"sort_order_number" => 1
"parent_id" => 1
"created_at" => null
"updated_at" => null
]
2 => array:12 [▼
"text_az" => "title2"
"text_en" => "title2.1"
"text_ru" => "title2.2"
"slug" => "fsdfsf"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 2
"sort_order_number" => 2
"parent_id" => 3
"created_at" => null
"updated_at" => null
]
3 => array:12 [▼
"text_az" => "title4"
"text_en" => "title4.1"
"text_ru" => "title4.2"
"slug" => "treteterter"
"icon" => "fa fa-bell"
"target" => "_top"
"id" => 4
"sort_order_number" => 2
"parent_id" => null
"created_at" => null
"updated_at" => null
]
]
【问题讨论】:
-
我假设您尝试了一些方法来提出您想要的结构,请在此处发布代码
-
使用递归,检查当前key是否为数组,如果是则添加到新数组中。
-
您能根据我的解释告诉我如何做到这一点吗?我需要 php 中的代码示例。我尝试了很多方法,但没有一个对我有用。 (
标签: php arrays laravel multidimensional-array laravel-5.5