2016-03-31 4 views
-1

私はツリー配列を持っていますが、ツリーの各レベルを独自のDIVに表示できるように、配列を次元でグループ化したいと思います。ディメンションレベルで配列をグループ化する方法は?

マイツリーの配列は次のとおりです。

$tree = 
array(
    1 => array('id'=>1, 'title'=>'Category 1', 'parent_id'=>0, 
     'children'=> array(
      2 => array('id'=>2, 'title'=>'Category 2', 'parent_id'=>1, 'children'=>''), 
      3 => array('id'=>3,'title'=>'Category 3','parent_id'=>1,'children'=>''), 
      4 => array('id'=>4, 'title'=>'Category 4', 'parent_id'=>1, 
       'children'=> array(
        8 => array('id'=>8, 'title'=>'Category 8', 'parent_id'=>4, 'children'=>''), 
        9 => array('id'=>9, 'title'=>'Category 9', 'parent_id'=>4, 'children'=>''), 
        10 => array('id'=>10, 'title'=>'Category 10', 'parent_id'=>4, 
         'children'=> array(
          11 => array('id'=>11, 'title'=>'Category 11', 'parent_id'=>10, 'children'=>''), 
          12 => array('id'=>12, 'title'=>'Category 12', 'parent_id'=>10, 'children'=>''), 
          13 => array('id'=>13, 'title'=>'Category 13', 'parent_id'=>10, 'children'=>'') 
         ) 
        ) 
       ) 
      ) 
     ) 
    ), 
    5 => array('id'=>5, 'title'=>'Category 5', 'parent_id'=>0, 
     'children'=>array(
      6 => array('id'=>6,'title'=>'Category 6', 'parent_id'=>5, 'children'=>''), 
      7 => array('id'=>3,'title'=>'Category 7', 'parent_id'=>5, 'children'=>'') 
     ) 
    ) 
); 

私はthisのように各レベルを分離したいと思います。グループ化された出力のための私の考えは、このようなものです:

$grouped = array(
    0 => array(
     1 => array('id'=>1, 'title'=>'Category 1', 'parent_id'=>0), 
     5 => array('id'=>5, 'title'=>'Category 5', 'parent_id'=>0) 
    ), 
    1 => array(
     2 => array('id'=>2, 'title'=>'Category 2', 'parent_id'=>1), 
     3 => array('id'=>3, 'title'=>'Category 3', 'parent_id'=>1), 
     4 => array('id'=>4, 'title'=>'Category 4', 'parent_id'=>1), 
     6 => array('id'=>6, 'title'=>'Category 6', 'parent_id'=>5), 
     7 => array('id'=>3, 'title'=>'Category 7', 'parent_id'=>5) 
    ), 
    2 => array(
     8 => array('id'=>8, 'title'=>'Category 8', 'parent_id'=>4), 
     9 => array('id'=>9, 'title'=>'Category 9', 'parent_id'=>4), 
     10 => array('id'=>10, 'title'=>'Category 10', 'parent_id'=>4) 
    ), 
    3 => array(
     11 => array('id'=>11, 'title'=>'Category 11', 'parent_id'=>10), 
     12 => array('id'=>12, 'title'=>'Category 12', 'parent_id'=>10), 
     13 => array('id'=>13, 'title'=>'Category 13', 'parent_id'=>10) 
    ) 
); 

あなたが他のアイデアを持っている場合、何か他のものは大丈夫ですが、私は、出力配列をforeachと異なる中で、各レベルのすべての要素を表示することができるようにしたいですDIV。

EX:

<div id="box-1"> 
    Group Array[1] 
</div> 
<div id="box-2"> 
    Group Array[2] 
</div> 
<div id="box-3"> 
    Group Array[3] 
</div> 
<div id="box-4"> 
    Group Array[4] 
</div> 

私はこれをどのように行うことができますか?

+2

[質問]と[完璧な質問](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)をご覧ください。 – Rizier123

+0

これは再帰的に実装できます。現在のレベルのすべての情報をリストに追加する関数を記述し、その関数内で関数自体を "children"プロパティで呼び出します。 – jojonas

+0

あなたの質問を編集して画像からコードを追加し、私があなたの意図する目標であると分かったものを明確にしました。あなたが念頭に置いたものでない場合は、変更を再編集したりロールバックしてください。また、これを達成するために書いたコードを追加することができれば、その質問をさらに明確にするのに役立ちます。 –

答えて

2

以下は、ツリー配列の各レベルを分離する単純な再帰関数の例です。

// begin with a reference to an empty array and level 0 
function split_levels($branches, &$output = array(), $level = 0) { 

    // loop over each element of the current branch 
    foreach ($branches as $id => $branch) { 

     // if the element has children, make a recursive call with an incremented level 
     if ($branch['children']) split_levels($branch['children'], $output, $level + 1); 

     // remove the children (this is optional) 
     unset($branch['children']); 

     // add the element to the output array at the appropriate level 
     $output[$level][$id] = $branch; 
    } 
} 

$outputは、この関数で参照されているので、それはこのように呼ばれるべきである。

split_levels($tree, $output); 

と結果が$outputになります。

+0

それは働く!!!! verry mushありがとう。 :) – user3514448

関連する問題