2012-02-15 8 views
2

カテゴリの配列があります。idはカテゴリのID、parentはカテゴリの親ID(id 0は最上位の親ノードを表します)、valueはアレイ。パスは、最初にカテゴリのIDに設定されます。配列は次のようになります。値が変数に代入されない

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [parent] => 0 
      [value] => Corporate Files 
      [path] => 1 
     ) 

    [1] => Array 
     (
      [id] => 2 
      [parent] => 0 
      [value] => Products Files 
      [path] => 2 
     ) 

    [2] => Array 
     (
      [id] => 3 
      [parent] => 1 
      [value] => Communications Materials 
      [path] => 3 
     ) 

    [3] => Array 
     (
      [id] => 4 
      [parent] => 1 
      [value] => Group Technical 
      [path] => 4 
     ) 

    [4] => Array 
     (
      [id] => 5 
      [parent] => 1 
      [value] => New Projects 
      [path] => 5 
     ) 

    [5] => Array 
     (
      [id] => 6 
      [parent] => 2 
      [value] => Product Range 
      [path] => 6 
     ) 

    [6] => Array 
     (
      [id] => 7 
      [parent] => 2 
      [value] => WL4 
      [path] => 7 
     ) 
); 

配列のカテゴリのパスを生成したいとします。出力は

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [parent] => 0 
      [value] => Corporate Files 
      [path] => 1 
     ) 

    [1] => Array 
     (
      [id] => 2 
      [parent] => 0 
      [value] => Products Files 
      [path] => 2 
     ) 

    [2] => Array 
     (
      [id] => 3 
      [parent] => 1 
      [value] => Communications Materials 
      [path] => 1,3 
     ) 

    [3] => Array 
     (
      [id] => 4 
      [parent] => 1 
      [value] => Group Technical 
      [path] => 1,4 
     ) 

    [4] => Array 
     (
      [id] => 5 
      [parent] => 1 
      [value] => New Projects 
      [path] => 1,5 
     ) 

    [5] => Array 
     (
      [id] => 6 
      [parent] => 2 
      [value] => Product Range 
      [path] => 2,6 
     ) 

    [6] => Array 
     (
      [id] => 7 
      [parent] => 2 
      [value] => WL4 
      [path] => 2,7 
     ) 
); 

次のように書きました。

function findparent($id,$path){ 
     global $categories; 
     global $catcnt; 

     if($id==0){ 
      echo $path."<br />"; //this outputs path currently 
      return $path; 
     } 
     for($i=0;$i<$catcnt;$i++){ 

      if($id==$categories[$i]['id']){ 
       $path=$id.",".$path; 
       findparent($categories[$i]['parent'],$path); 
      } 
     } 
    } 

for($i=0;$i<count($categories);$i++){ 
      $categories[$i]['path']=(string)findparent($categories[$i]['parent'],$categories[$i]['id']); //this doesnt assign it currectly 

    } 

、出力は次のとおりです。

私は間違っているつもりです
Array 
(
    [0] => Array 
     (
      [id] => 1 
      [parent] => 0 
      [value] => Corporate Files 
      [path] => 
     ) 

    [1] => Array 
     (
      [id] => 2 
      [parent] => 0 
      [value] => Products Files 
      [path] => 
     ) 

    [2] => Array 
     (
      [id] => 3 
      [parent] => 1 
      [value] => Communications Materials 
      [path] => 
     ) 

    [3] => Array 
     (
      [id] => 4 
      [parent] => 1 
      [value] => Group Technical 
      [path] => 
     ) 

    [4] => Array 
     (
      [id] => 5 
      [parent] => 1 
      [value] => New Projects 
      [path] => 
     ) 

    [5] => Array 
     (
      [id] => 6 
      [parent] => 2 
      [value] => Product Range 
      [path] => 
     ) 

    [6] => Array 
     (
      [id] => 7 
      [parent] => 2 
      [value] => WL4 
      [path] => 
     ) 
); 

+0

'findparent'関数の中で間違っていると思います。 「親ID値に基づいて配列を1次元から多次元に変換する」(http://stackoverflow.com/a/7768445/367456)の私の答えも同様であり、どのようにデータを集めることができるかを示します。 – hakre

答えて

2

findparentは、idがゼロの場合のみ返します。

再帰的なfindparentコールの前に、2番目のreturn文が必要です。

+0

ありがとうございました:) –

1

あなたは複数のレベルで対処する必要がないので、あなたが簡単なforeachで配列自体にまっすぐな仕事ができるよう、それ自身の機能は、オーバーヘッドのビットです:

foreach ($array as &$node) 
{ 
     if ($node['parent']) 
     { 
       $node['path'] = $node['parent'] . ',' . $node['path']; 
     } 
} 
unset($node); 

しかし、これを自分自身の関数に入れることもできますが、私が見る限り、グローバル変数は必要ありません。

ここに表示されているのは、単純な文字列の連結です。これは、あなたが望むように書いた配列になります。私の最初のコメントは、1深度ではなく、n深度で構造を管理することを意味していました。 Demo

関連する問題