English 中文(简体)
在“父母”的基础上,将一个阵列纳入一个多层面阵列
原标题:Sorting an array into a multidimensional array based on "parent_id"

I m 通过向每个“album”分配一个“父母-album_id”(除非在最高一级,在这种情况下,它没有父母一方。

首先,我从数据库中抽取了一系列的书目,并修改了每个书目中“id”(autoincrement id)。

其次,我要将各种小册子重新组织成多功能阵列,为每个书目保存儿童歌。 我已经取得了一些成功。 如果我只需要降低一层阵列,但如果我降级超过一层,就会失去阵列的全部结构,那么以下的法典就行了。 这是因为,当我直截了当的阵列——研究——我没有穿过整个阵列,只是我想要找的下一个层次。

我如何能够通过阵列重新搜索,但回归整个多维系列的小册子?

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;
}
最佳回答
// 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 ], "
";
      }
      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
        )

      )

    )

)
问题回答

暂无回答




相关问题
How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

Can I Nest OrderBy in .NET?

This doesn t seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m) m.Sent).First.Sent) For Each d As Discussion In x ....

sorting elements javascript

I m looking for a way to sort my elements, but it isn t as easy as it sounds. Please let me explain My elements are grouped per 6 elements (thumbnails), each x represents a thumbnail However all ...

热门标签