按元素合并数组';PHP中的父\u id
我不确定PHP中是否已经有人问过这个问题,但我找不到答案 我有一个列有id和父id的数组按元素合并数组';PHP中的父\u id,php,arrays,Php,Arrays,我不确定PHP中是否已经有人问过这个问题,但我找不到答案 我有一个列有id和父id的数组 $cats = [▼ 0 => array:4 [▼ "id" => 5 "parent_id" => 4 "category" => "Detroit" "children" => [] ] 1 => array:4 [▼
$cats = [▼
0 => array:4 [▼
"id" => 5
"parent_id" => 4
"category" => "Detroit"
"children" => []
]
1 => array:4 [▼
"id" => 4
"parent_id" => 3
"category" => "Michigan"
"children" => []
]
2 => array:4 [▼
"id" => 2
"parent_id" => 1
"category" => "Cannes"
"children" => []
]
3 => array:4 [▼
"id" => 1
"parent_id" => 0
"category" => "France"
"children" => []
]
4 => array:4 [▼
"id" => 3
"parent_id" => 0
"category" => "United States"
"children" => []
]
]
我想按父id向后循环,以将孙辈合并到子代中,将子代合并到父代中
我尝试使用数组搜索按id查找父对象,然后将子对象推入父对象的子对象数组
foreach ($cats as $element){
//If not the root level
if($element['parent_id']!==0){
//Find parent index
$parent_index = array_search($element['parent_id'],array_column($cats,'id'));
//Push Child
array_push($cats[$parent_index]['children'],$element);
}
}
我想我需要在将原始子元素推入父数组后取消设置它,但似乎当子元素被推入父元素时,孙子元素丢失了(正如您可以看到的,在美国[index 4]的最后一个条目中有密歇根,但没有底特律),我认为它应该在索引2中更新
array:5 [▼
0 => array:4 [▼
"id" => 5
"parent_id" => 4
"category" => "Detroit"
"children" => []
]
1 => array:4 [▼
"id" => 4
"parent_id" => 3
"category" => "Michigan"
"children" => array:1 [▼
0 => array:4 [▼
"id" => 5
"parent_id" => 4
"category" => "Detroit"
"children" => []
]
]
]
2 => array:4 [▼
"id" => 2
"parent_id" => 1
"category" => "Cannes"
"children" => []
]
3 => array:4 [▼
"id" => 1
"parent_id" => 0
"category" => "France"
"children" => array:1 [▼
0 => array:4 [▼
"id" => 2
"parent_id" => 1
"category" => "Cannes"
"children" => []
]
]
]
4 => array:4 [▼
"id" => 3
"parent_id" => 0
"category" => "United States"
"children" => array:1 [▼
0 => array:4 [▼
"id" => 4
"parent_id" => 3
"category" => "Michigan"
"children" => []
]
]
]
]
我很感激任何关于我做错了什么或为什么这不起作用的提示或答案。谢谢 每次看到任意嵌套的数组,我都会想到递归。只能通过数组中的一个循环执行此操作 这就是我的方法:在数组中循环查找子数组,并将它们附加到新数组中,第一次添加到空数组中,连续多次递归地添加到子数组中
$cats = [
[
"id" => 5,
"parent_id" => 4,
"category" => "Detroit",
"children" => [],
],
[
"id" => 4,
"parent_id" => 3,
"category" => "Michigan",
"children" => [],
],
[
"id" => 2,
"parent_id" => 1,
"category" => "Cannes",
"children" => [],
],
[
"id" => 1,
"parent_id" => 0,
"category" => "France",
"children" => [],
],
[
"id" => 3,
"parent_id" => 0,
"category" => "United States",
"children" => [],
]
];
$out = [];
function get_children($in, &$out, $parent){
foreach ($in as $item) {
if ($parent == $item["parent_id"]){
$out[] = $item;
get_children($in, $out[count($out)-1]["children"], $item["id"]);
}
}
}
get_children($cats, $out, 0);
print_r($out);
//OUTPUT:
Array
(
[0] => Array
(
[id] => 1
[parent_id] => 0
[category] => France
[children] => Array
(
[0] => Array
(
[id] => 2
[parent_id] => 1
[category] => Cannes
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 3
[parent_id] => 0
[category] => United States
[children] => Array
(
[0] => Array
(
[id] => 4
[parent_id] => 3
[category] => Michigan
[children] => Array
(
[0] => Array
(
[id] => 5
[parent_id] => 4
[category] => Detroit
[children] => Array
(
)
)
)
)
)
)
)
这可能不是最有效的解决方案,我也没有足够的知识来告诉你时间或空间的复杂性,但是,嘿,它是有效的
编辑
我后来才意识到,当您将元素添加到新数组中时,可以从原始数组中删除元素,这将使其性能更高:
function get_children(&$in, &$out, $parent){
foreach ($in as $key=>$item) {
if ($parent == $item["parent_id"]){
$out[] = $item;
unset($in[$key]);
get_children($in, $out[count($out)-1]["children"], $item["id"]);
}
}
}
向我们显示
echo var_export($cats)的输出代码>因此我们可以复制粘贴和复制array(0=>array('id'=>5,'parent'=>4,'category'=>底特律,'children'=>array(),),1=>array('id'=>4,'parent'=>3,'category'=>Michigan','children'=>array(),),2=>array('id'=>2,'parent'=>1,'category'=>Cannes','children=>array(),),3=>array('id'=>1,'parent\u id'=>0,'category'=>'France','children'=>array(),),4=>array('id'=>3,'parent\u id'=>0,'category'=>'us','children'=>array(),),)孙子在哪里?他们将在循环中创建。随着Detroit添加到Michigan,那么当.Michigan添加到美国时,Detroit将是孙子。我正在尝试将所有具有父元素的元素压缩到数组中,以便只保留父元素id为0的元素s