I have a tree-like array and I need to marge or convert it to a flat array but I cannot make it work.
I am trying with this code:
$fakepages = array();
$fakepages[0] = array('id' => 1, 'parent_id' => 0, 'title' => 'Parent Page');
$fakepages[1] = array('id' => 2, 'parent_id' => 1, 'title' => 'Sub Page');
$fakepages[2] = array('id' => 3, 'parent_id' => 2, 'title' => 'Sub Sub Page');
$fakepages[3] = array('id' => 4, 'parent_id' => 3, 'title' => 'Another Parent Page');
$fakepages[4] = array('id' => 5, 'parent_id' => 0, 'title' => 'Another Parent Page 5');
$fakepages[5] = array('id' => 6, 'parent_id' => 2, 'title' => 'Another Parent Page 5');
$tree = $this->buildTree($fakepages);
return $tree;
private function buildTree(array $elements, $parentId = 0) {
$branch = array();
foreach ($elements as $element) {
if ($element['parent_id'] == $parentId) {
$children = $this->buildTree($elements, $element['id']);
if ($children) {
$element['children'] = $children;
}
$branch[] = $element;
}
}
return $branch;
}
And this is my sort (or merge or ...?) code. What's is the problem with my code and how can I fix this and make it like in the left picture?
private function findchild($trees)
{
$arr = array();
foreach ($trees as $tree) {
if (isset($tree['children'])) {
$this->mes[] = ['id'=>$tree['id'],'parent_id'=>$tree['parent_id']];
$arr[] = $this->findchild($tree['children']);
}
else{
$this->mes[] = ['id'=>$tree['id'],'parent_id'=>$tree['parent_id']];
$arr[] = $tree;
}
return $arr;
}
}
private $mes = [];
I have what is in the right part, but need to convert it to what is in the left part:
