Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9142151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:44:22+00:00 2026-06-17T09:44:22+00:00

So i have this issue regarding recursive arrays. What i want is to get

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:44:23+00:00Added an answer on June 17, 2026 at 9:44 am

    In getPages, if you want it to return an associative array, then you need to do:

    return $query->fetchAll(PDO::FETCH_ASSOC);
    

    and then your getMenu would look like:

    function getMenu($tree = null, $parent = 0) {
        if (!isset($tree))
            $tree = getPages();
    
        $tree2 = array();
        foreach($tree as $i => $item) {
            if($item['id'] == $parent) {
                $tree2[$item['id']] = $item;
                $tree2[$item['id']]['submenu'] = getMenu($tree, $item['id']);
            }
        }
    
        return $tree2;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue regarding Sendkeys Class, as i want to use this class
I have looked at the other posts from the other folks regarding this issue
There have been a few questions regarding this issue before; my understanding is that
I have seen lot of questions regarding this issue.I think from myside I have
I have seen a few posts regarding this issue but not one specific to
I have looked at the different questions regarding this issue, but couldn't find anything
I have gone through many posts on SO regarding this issue: Tried everything in
I know there are tons of threads regarding this issue but I have not
I just have this issue with reading from a network stream in C#. Since
I've been reading similar questions to this issue and have been able to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.