2011-02-08 10 views
1

で再帰を作成する方法。私は多くの記事を読んだが解決策は出てこない。PHPの再帰:私はPHPで再帰で少し問題を抱えているPHP

私はこの配列を持っている:

[59] => Array 
    (
     [ID] => REL000000 
     [Name] => RELIGIONE/Generale 
     [Description] => 
     [IdParent] => 
    ) 

[799] => Array 
    (
     [ID] => REL102000 
     [Name] => RELIGIONE/Teologia 
     [Description] => 
     [IdParent] => REL000000 
    ) 

[800] => Array 
    (
     [ID] => REL068000 
     [Name] => RELIGIONE/Teosofia 
     [Description] => 
     [IdParent] => REL000000 
    ) 

[801] => Array 
    (
     [ID] => REL103000 
     [Name] => RELIGIONE/Universalismo Unitario 
     [Description] => 
     [IdParent] => REL000000 
    ) 

[802] => Array 
    (
     [ID] => REL034000 
     [Name] => RELIGIONE/FestivitÃ/Generale 
     [Description] => 
     [IdParent] => REL000000 
    ) 

私はIDフィールドでIdParentフィールドの一致階層ツリーを作成したいと思います。

誰も私を助けていますか?

$array[$id_child]['parent'] = &$array[$id_parent]; 

とも::

$array[$id_parent]['children'][] = &$array[$id_child]; 

答えて

2

&演算子を使用しますか?私はコードを書くいる

+0

?それははっきりしていません。 – Michelangelo

+0

あなたのコードの残りの部分を私に提供しない限り、どこに置くべきかわかりません。上記の2行で、$配列は、それぞれ、配列のキーですid_parentあなたが質問で提供配列は、$ id_childと$で、子の親の(つまり$配列[$ id_child] $配列の子であります[$ id_parent]、逆の場合は後者が前者の親です)。 – CAFxX

0
a[59]['IdParent'] = a[59]['ID']; 

は、あなたの質問ごとにその仕事をしません

おかげ

0
// There is a function maybe useful 
/** 
* get all sub 
* 
* @author Tom 
* @date 2017-12-21 
* @param array $array The array 
* @param int|str $topId the the top ID 
* @param int  $lev the the lev(It's ver useful in some case) 
* @return array $result all sub data 
*/ 
function getLower($array, $topId, $lev = 0) { 
    $result = []; 
    foreach ($array as $key => $value) { 
     if ($value['IdParent'] == $topId) { 
      $value['lev'] = $lev; // the level 
      $result[]  = $value; 
      $result  = array_merge($result, getLower($array, $value['ID'], $lev + 1)); 
     } 
    } 
    return $result; 
} 
+1

あなたの答えと一緒にいくつかの説明を追加するといいです –

+0

あなたの助言のためにタンク。私の英語はとてもそうだから。だから、私にとってはとても難しいです。しかし、私が自由になると、私はそれをするために最善を尽くします。 –