i am trying to create a multidimensional array hierarchy from a simple array which contains pairs of category ids and parent ids. The categories can be a parent and a subcategor
Get your list :
0,
2 => 1,
3 => 2,
4 => 0,
5 => 4,
6 => 0
);
$menus = array();
function generateMenus($start) {
global $initialArray;
foreach($initialArray as $k => $v) {
if($start == $v) {
$menus[] = $k;
}
}
return $menus;
}
$final = array();
foreach($initialArray as $key => $value) {
$final[$value] = generateMenus($value);
}
echo '';
function generateList($start) {
global $final;
echo '- '.$start.'
';
if(!empty($final[$start])) {
echo '';
foreach($final[$start] as $v) {
generateList($v);
}
echo '
';
}
}
generateList(0);
echo '
';
?>