I am bit stuck with my following code as it is not including an element inside the array which has no child.My code is
$arr = array
(
array
(
'id' => 66,
'parent_id' => 26,
'ptitle' => 'slider container',
)
,
array
(
'id' => 47,
'parent_id' => 26,
'ptitle' => 'Landing Page',
)
,
array
(
'id' => 48,
'parent_id' => 66,
'ptitle' => 'Link 1',
)
,
array
(
'id' => 49,
'parent_id' => 66,
'ptitle' => 'Link 2',
)
,
);
$new = array();
foreach ($arr as $a){
$new[$a['parent_id']][] = $a;
}
$tree = createTree($new, array($arr[0]));
echo "<pre>";
print_r($tree);
echo "</pre>";
function createTree(&$list, $parent){
$tree = array();
foreach ($parent as $k=>$l){
if(isset($list[$l['id']])){
$l['children'] = createTree($list, $list[$l['id']]);
}
$tree[] = $l;
}
return $tree;
}
As the looping starts from first element of the array which is $arr[0] so that's why it is not considering the element whose id is 47 but I need this to be included in final array.
Any help is highly appreciated.
SOLVED