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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:57:13+00:00 2026-06-18T07:57:13+00:00

I am looking for an example how a recursive array passed as parameter for

  • 0

I am looking for an example how a recursive array passed as parameter for RecursiveArrayIterator should be structured. I have a mysql table with id, parent_id and title:

id    parent_id   title
-----------------|-------------------
1         0      | Item 1
2         1      | Item 2
3         1      | Item 3
4         3      | Item 4
5         4      | Item 5

I would like to create an array from the table, that can be passed to RecursiveArrayIterator to build trees, menus, options and so on. I have tried this “build_tree” function https://stackoverflow.com/a/8587437/1746522 to create an array from mysql table. The resulted array looks like:

array (size=2)
  0 => 
    array (size=3)
      'id' => string '1' (length=1)
      'title' => string 'Item 1' (length=6)
      'parent_id' => string '0' (length=1)
      'children' => 
        array (size=2)
          0 => 
            array (size=3)
              ...
          1 => 
            array (size=3)
              ...

But when I pass the resulted array to RecursiveArrayIterator it looks strange. E.g. using this function:

$array = new RecursiveArrayIterator($tree);
$iterater = new RecursiveIteratorIterator($array);  
foreach ($iterater as $key => $value) {
    $d = $iterater->getDepth();            
    echo "depth=$d k=$key v=$value\n";
}

I have the strange and unusable output like:

depth=0 k=0 v=Array
depth=1 k=id v=1
depth=1 k=title v=Item 1
depth=1 k=parent_id v=0
depth=2 k=0 v=Array
depth=3 k=id v=2
depth=3 k=title v=Item 2
depth=3 k=parent_id v=1
depth=4 k=0 v=Array
....

The depth is wrong, and it iterates through single values and not elements. Probably is the array passed to RecursiveArrayIterator is “malformed” and should be structured other way?

I have expected to see something like this:

depth=0 k=1 v=Array
depth=1 k=2 v=Array
depth=1 k=3 v=Array

where Array contains all values to the node, like parent_id and title and k is the id from the mysql column id.

  • 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-18T07:57:14+00:00Added an answer on June 18, 2026 at 7:57 am

    My final solution with unlimited depth, only one query, all information from the table and access to all functions of RecursiveIteratorIterator like children and depth. I use two arrays in my solution.

    Flat array

    select * from table
    

    and create flat array as usual:

    $info[1] = array('parent_id' => 0, 'title' => 'Item 1');
    $info[2] = array('parent_id' => 1, 'title' => 'Item 2');
    $info[3] = array('parent_id' => 1, 'title' => 'Item 3');
    $info[4] = array('parent_id' => 3, 'title' => 'Item 4');
    $info[5] = array('parent_id' => 4, 'title' => 'Item 5');
    

    use id column as array index.

    Nested Array

    Create nested array from $info that contains ONLY ids of the items:

    function tree_structure($info, $parent = 0) {    
        foreach ($info as $row) {
            if ($row['parent_id'] == $parent)
                $struc[$row['id']] = tree_structure($info, $row['id']);
        }    
        return $struc;        
    }
    

    This array can be passed to RecursiveArrayIterator.

    You have two arrays now:

    • $info – (flat) contains all information about your nodes
    • $struc – (recursive) contains the structure of the nodes

    RecursiveArrayIterator

    Start to work with RecursiveArrayIterator

        $array = new RecursiveArrayIterator($struc);
        $iterator = new RecursiveIteratorIterator($array, TRUE);
        $iterator->rewind();
    
        while ($iterator->valid()) {
            // Get the id
            $iterator->key();
            // Get the depth
            $iterator->getDepth();
            // Check if it has children
            $iterator->hasChildren();
            // Get the number of children
            sizeof($iterator->callGetChildren());
            // Get all information for the node from the flat array
            $info[$iterator->key()];
    
            $iterator->next();
        }
    

    The line $info[$iterator->key()] in the code does the actual trick. With this line you have all information about your node without messing the structure.

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

Sidebar

Related Questions

I was looking for an example how to store a large text array (
I'm looking for a few recursive function examples, preferably ones that show increase in
I'm looking for example code for setting up a connection to a bluetooth device
I'm looking for example of how I would solve the scenario below: Imagine my
I'm trying to learn Rails better by looking at example applications, and while looking
Looking for an example or documentation links as to how to implement a method
looking at this example of the jquery ui slider http://jqueryui.com/demos/slider/#steps i want to be
im looking for an example script. I saw one yesterday but for the life
I was looking at the example in Microsoft KB318804 but they use the threadId
I am looking for simple example code for setting up a Message in an

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.