So i have this issue regarding recursive arrays. What i want is to get an recursive array with PHP from my database so that i can create a navigation menu with submenu’s without creating another table.
this is the function i have so far;
function getMenu($tree = null){
$tree2 = array();
$tree = getPages();
foreach($tree as $i => $item){
if($item->parent_id == $item->ID){
$tree2[$item->ID] = $item;
$tree2[$item->ID]['submenu'] = getMenu($tree, $item->ID);
}
}
return $tree2;
}
To make things clear i did not create this function myself but got it from http://www.jugbit.com/php/php-recursive-menu-with-1-query/ and made adjustments where i thought where the right one
The $tree variable is coming from this function;
function getPages($limit = null,$sort = null) {
global $db;
$query = $db->prepare('SELECT * FROM pages ORDER BY Position');
$query->execute();
return $query->fetchAll(PDO::FETCH_CLASS);
}
Now i have been busting my balls over this for the last two days trying to figure out what i am doing wrong.
if i print the getMenu function all i get is an empty array wich i can’t figure out why. it does get the foreach right and i don’t think that’s the issue but i’m not 100% sure..
i hope the question is clear but if it’s not i’m sorry and will clarify where needed.
Thanks in advance
In
getPages, if you want it to return an associative array, then you need to do:and then your
getMenuwould look like: