I've looked around on this site, elsewhere on the internet and i have tried quite some things, but can't get my head around the principle of building an hierarchical menu tree from an array.
I have an array like this:
$categories = array (
0 =>
array(
'term_id' => '1413',
'name' => 'Parent',
'parent' => '0',
)
,
19 =>
array(
'term_id' => '2743',
'name' => 'Child',
'parent' => '1413',
)
,
21 =>
array(
'term_id' => '2744',
'name' => 'Grand child',
'parent' => '2743',
)
,
22 =>
array(
'term_id' => '2738',
'name' => 'Grand Child',
'parent' => '2716',
)
,
25 =>
array(
'term_id' => '2716',
'name' => 'Child',
'parent' => '1413',
)
,
33 =>
array(
'term_id' => '2722',
'name' => 'Child',
'parent' => '1413',
)
,
41 =>
array(
'term_id' => '2720',
'name' => 'Grand child',
'parent' => '2716',
)
,
58 =>
array(
'term_id' => '2717',
'name' => 'Grand child',
'parent' => '2716',
)
,
618 =>
array(
'term_id' => '3321',
'name' => 'Grand child',
'parent' => '2743',
)
);
I now want to build a menu with an output like:
Parent - Child - Grand child - Grand child - Child - Grand child - Grand child Parent - Child - Grand child - Grand child - Child - Grand child - Grand child
The closest i get is with the code below, but i just have no clue what i'm doing.
$hasChild = 0;
$idCount = array();
function buildTree($arr, $parent = 0){
global $hasChild;
global $idCount;
foreach($arr as $item){
if($item['parent'] == 0 && $hasChild == 0 && !in_array($item['term_id'], $idCount)){
array_push($idCount, $item['term_id']);
echo $item['name'];
$hasChild = 1;
buildTree($arr, $item['parent']);
}else{
if(!in_array($item['term_id'], $idCount)){
echo '<br>- '.$item['name'];
}
}
}
}
buildTree($categories, 0);
Allthough i'm happy i finally get an actual output without errors, the list with duplicates doesn't help me.
I'm really hoping someone can push me in the right direction because i just have no clue (what i'm doing)...