基于“parent_id”将数组排序为多维数组

时间:2010-04-05 18:33:39

标签: php arrays sorting multidimensional-array

我通过为每个“专辑”分配一个“parent_album_id”来在mysql中存储一个无限嵌套的目录结构(除非它位于顶层,在这种情况下它没有parent_album_id)。

我首先从数据库中获取所有相册的数组,并将每个相册键更改为“id”(自动增量ID)。

接下来,我想将相册数组重新组织成一个多维数组,将儿童相册存储在每个相册的“儿童”中。我取得了一些成功。如果我只需要在数组中向下一个级别,下面的代码工作正常,但如果我下降多个级别,它将失去数组的完整结构。这是因为当我递归调用array_search_key时,我没有传递完整的数组,只是我要搜索的下一个级别。

如何递归搜索数组,但返回整个多维数据的专辑?

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 个答案:

答案 0 :(得分: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;
}

打印

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
        )

      )

    )

)