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 797527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:49:14+00:00 2026-05-14T22:49:14+00:00

When retrieving a hierarchical structure from MySQL (table with one ID column and one

  • 0

When retrieving a hierarchical structure from MySQL (table with one ID column and one PARENT column signifying the hierarchical relationships), I map the result into an enumerated array as follows (for this example the numbers are arbitrary):

Array ( [3] => Array ( [7] => Array () ), [7] => Array ( [8] => Array () ) )

Notice 3 is the parent of 7, and 7 is the parent of 8 (this could go on and on; and any parent could have multiple children).

I wanted to shrink this array into a nested multidimensional array as follows:

Array ( [3] => Array ( [7] => Array ( [8] => Array () ) ) )

That is, each NEW id is automatically assigned an empty array. Regardless, any ID’s children will be pushed into their parent’s array.

Take a look at the following illustration for further clarification:

alt text http://img263.imageshack.us/img263/4986/array.gif

This will probably result in a complicated recursive operation, since I always have to check whether a parent with any certain ID already exists (and if so, push the value into its array).

Is there a built-in php function that can assist me with this? Do you have any idea as to how to go about constructing this? For what it’s worth I’m using this to built a navigation bar in wordpress (which can contain categories, subcategories, posts… essentially anything).

  • 1 1 Answer
  • 2 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-05-14T22:49:14+00:00Added an answer on May 14, 2026 at 10:49 pm

    The idea is that you keep an auxiliary array with all the nodes (parent and child) you find. The values of this arrays are references that back your result.

    This builds the tree in linear time (array_key_exists does a hash table lookup, which is on average O(1)):

    //table contains (id, parent)
    $orig = array(
        11 => 8,
        7 => 3,
        8 => 7,
        99 => 8,
        16 => 8,
    );
    
    $childrenTable = array();
    $result = array();
    
    foreach ($orig as $n => $p) {
        //parent was not seen before, put on root
        if (!array_key_exists($p, $childrenTable)) {
            $childrenTable[$p] = array();
            $result[$p] = &$childrenTable[$p];
        }
        //child was not seen before
        if (!array_key_exists($n, $childrenTable)) {
            $childrenTable[$n] = array();
        }
    
        //root node has a parent after all, relocate
        if (array_key_exists($n, $result)) {
            unset($result[$n]);
        }
    
        $childrenTable[$p][$n] = &$childrenTable[$n];
    }
    unset($childrenTable);
    
    var_dump($result);
    

    gives

    array(1) {
      [3]=>
      array(1) {
        [7]=>
        array(1) {
          [8]=>
          array(3) {
            [11]=>
            array(0) {
            }
            [99]=>
            array(0) {
            }
            [16]=>
            array(0) {
            }
          }
        }
      }
    }
    

    EDIT: unset $childrenTable in the end to clear reference flags. In practice, you will probably want to do the operation inside a function anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm retrieving data from mysql like this structure => echo <table id='postI . $chat_row['id']
I'm retrieving data from a MySQL table and displaying it on a webpage using
I'm retrieving data from a table through VBA.The select query gives output correctly.One of
I'm retrieving a set of location names from a MySQL database using PHP and
When retrieving data from MySQL, the PHP array output has both numeric and name
retrieving items from table that fall between a date range. the date (db table
I am retrieving a file from our FTP server which is then put into
I am retrieving the datetime data from mysql the retrieved data from a single
When retrieving a lookup code value from a table, some folks do this... Dim
When retrieving values from a DataRow is it better to use the column name

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.