2

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 Answer 1

1
// 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
        )

      )

    )

)
Sign up to request clarification or add additional context in comments.

3 Comments

Something is wrong with that function. In the returned array the album with the ID of 5 appears multiple times with different titles.
The script works fine. But I pasted the output from a script version where multiple elements had 'id'=>5 (copy&paste error). Corrected.
Thanks, works great! Only change I made was to at the very end loop through the array and remove all top level albums with a parent_id (since they have already been inserted into the correct parent).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.