我需要一个数组,该数组将每个元素的键(由/
分割)变成一个子数组,并在新数组中以正确的格式分配数据。可以有多个级别的嵌套,实际上从不超过10个级别,但这有待确定。
例如;给定
的输入$i_have_this = [
"Base/child" => [
[
"filename" => "child-1",
"last_modified" => "29/01/2020"
],
[
"filename" => "child-2",
"last_modified" => "29/01/2020"
],
[
"filename" => "child-3",
"last_modified" => "29/01/2020"
]
],
"Base/child/grandChild1" => [
[
"filename" => "grandChild1-1",
"last_modified" => "29/01/2020"
]
],
"Base/child/grandChild2" => [
[
"filename" => "grandChild2-1",
"last_modified" => "29/01/2020"
],
[
"filename" => "grandChild2-2",
"last_modified" => "29/01/2020"
],
[
"filename" => "grandChild2-3",
"last_modified" => "29/01/2020"
],
[
"filename" => "grandChild2-4",
"last_modified" => "29/01/2020"
],
[
"filename" => "grandChild2-5",
"last_modified" => "29/01/2020"
]
]
];
我想要]的输出>
$want_this = [ 'name' => 'Base', 'children' => [ [ 'name' => 'child', 'children' => [ ["name" => "child-1"], ["name" => "child-2"], ["name" => "child-3"], [ "name" => "grandChild1", "children" => [ ["name" => "grandChild1-1"] ] ], [ "name" => "grandChild2", "children" => [ ["name" => "grandChild2-1"], ["name" => "grandChild2-2"], ["name" => "grandChild2-3"], ["name" => "grandChild2-4"] ] ], ] ] ] ];
到目前为止,我有;
foreach($i_have_this as $path => $value) { $temp = &$want_this; foreach (explode('/', $path) as $key) { $temp = &$temp[$key]; } $temp = $value; }
但还不能完全结束。
我需要一个数组,该数组将每个元素的键(由/分隔)变成一个子数组,并在新数组中以正确的格式分配数据。可以有多层嵌套,...
我认为您可以像大多数人对数组使用“点”表示法一样对待这种方式(例如在Laravel中)。只需替换“。”在您的情况下带有“ /”: