I'm trying to do recursive array in function to get all level number and the total element counts on each level for a multi-dimensional array.
I need help to achieve this goal I'm stack and can't come up of good solution.
My data
$tree = array(
'room1' => array(
'room5',
'room6'
),
'room2' => array(
'room5',
'room6'
),
'room3' => array(
'room7' => array(
'room12' => array(
'room14',
'room15'
),
'room13'
),
),
'room4' => array(
'room8',
'room9',
'room10',
'room11'
)
);
Desired Result
Array(
'level1' => 4,
'level2' => 9,
'level3' => 2,
'level4' => 2
)
My Code
function treeOut($tree)
{
$markup = '';
$count = 0;
foreach($tree as $branch => $twig)
{
$count++;
((is_array($twig)) ? treeOut($twig,$count) : $count++;
}
return $count
}
echo treeOut($tree);