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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:33:13+00:00 2026-05-14T05:33:13+00:00

I’m storing an infinitely nested directory structure in mysql by assigning a parent_album_id to

  • 0

I’m storing an infinitely nested directory structure in mysql by assigning a “parent_album_id” to each “album” (unless it’s at the top level, in which case it does not have a parent_album_id).

I first grab an array of all the albums from the database and change each albums key to it’s “id” (autoincrement id).

Next, I want to reorganize the array of albums into a multidemensional array, storing child albums in ‘children’ for each album. I’ve had some success. The following code works fine if I only need to go down one level in the array, but if I go down more than one level it loses the full structure of the array. This is because when I recursively call array_search_key I don’t pass the full array, just the next level that I want to search.

How can I recursively search through the array, but return the entire multidimensional array of albums?

foreach ($albums as &$album){
    if($album['parent_album_id']){ // Move album if it has a parent
        $insert_album = $album;
        unset($albums[$album['id']]); // Remove album from the array, since we are going to insert it into its parent
        $results = array_search_key($album['parent_album_id'],$albums,$insert_album, $albums);
        if($results){
            $albums = $results;
        }
    }
}

function array_search_key( $needle_key, $array , $insert_album) {
   foreach($array AS $key=>&$value){
       if($key == $needle_key) {
           $array[$key]['children'][$insert_album['id']] = $insert_album;
           return $array;
       }
       if(is_array($value) && is_array($value['children'])){    
           if( ($result = array_search_key($needle_key, $value['children'], $insert_album)) !== false)
           return $result;
       }
   }
   return false;
}
  • 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-05-14T05:33:14+00:00Added an answer on May 14, 2026 at 5:33 am
    // you already managed to get the array into this form
    $albums = array(
      1 => array('id'=>1, 'title'=>'Album 1', 'parentId'=>null),
      2 => array('id'=>2, 'title'=>'Album 2', 'parentId'=>null),
      3 => array('id'=>3, 'title'=>'Album 1.1', 'parentId'=>1),
      4 => array('id'=>4, 'title'=>'Album 1.1.1', 'parentId'=>3),
      5 => array('id'=>5, 'title'=>'Album 2.1', 'parentId'=>2),
      6 => array('id'=>6, 'title'=>'Album 1.1.2', 'parentId'=>3),
      7 => array('id'=>7, 'title'=>'Album 1.1.3', 'parentId'=>3)
    );
    
    print_r(foo($albums));
    
    
    function foo($albums) {
      $rv = array();
      foreach( $albums as &$album) {
        if ( is_null($album['parentId']) ) {
          // no parentId -> entry in the root array
          $rv[] = &$album;
        }
        else {
          $pid = $album['parentId'];
          if ( !isset($albums[$pid]) ) {
            echo 'orphant album: ', $album['id'], "\n";
          }
          else {
            if ( !isset($albums[$pid]['children']) ) {
              $albums[$pid]['children'] = array();
            }
            $albums[$pid]['children'][] = &$album;
          }
        }
      }
      return $rv;
    }
    

    prints

    Array
    (
      [0] => Array
        (
        [id] => 1
        [title] => Album 1
        [parentId] => 
        [children] => Array
          (
            [0] => Array
            (
              [id] => 3
              [title] => Album 1.1
              [parentId] => 1
              [children] => Array
                (
                [0] => Array
                  (
                    [id] => 4
                    [title] => Album 1.1.1
                    [parentId] => 3
                  )
    
                [1] => Array
                  (
                    [id] => 6
                    [title] => Album 1.1.2
                    [parentId] => 3
                  )
    
                [2] => Array
                  (
                    [id] => 7
                    [title] => Album 1.1.3
                    [parentId] => 3
                  )
    
                )
    
            )
    
          )
    
        )
    
      [1] => Array
        (
        [id] => 2
        [title] => Album 2
        [parentId] => 
        [children] => Array
          (
            [0] => Array
            (
              [id] => 5
              [title] => Album 2.1
              [parentId] => 2
            )
    
          )
    
        )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.