2011-01-19 15 views
2

私は議論の多次元配列を持っています。ディスカッション掲示板の多次元配列

私はこの配列の値(コメント)をエコーするために再帰関数(下記参照)を使用しています。しかし、私の関数では、最初の子コメント(配列レベルごと)だけが表示されます。

普通のディスカッション掲示板のように、アレイレベルごとにすべての子コメントをエコーバックできるように、この機能をどのように適応させることができますか?

この例の配列では、comment_id "4"とcomment_id "7"は同じレベルにありますが、現在の関数ではcomment_id "4"のコメントしか表示されません。

Array 
(
    [0] => Array 
     (
      [comment_id] => 1 
      [comment_content] => This is a comment... 
      [child] => Array 
       (
        [0] => Array 
         (
          [comment_id] => 3 
          [comment_content] => This is a reply to the comment... 
          [child] => Array 
           (
            [0] => Array 
             (
              [comment_id] => 4 
              [comment_content] => This is a reply to the reply... 
              [child] => Array 
               (
               ) 
             ) 

            [1] => Array 
             (
              [comment_id] => 7 
              [comment_content] => This is a another reply to the reply... 
              [child] => Array 
               (
               ) 
             ) 
           ) 
         ) 
       ) 
     ) 

    [1] => Array 
     (
      [comment_id] => 2 
      [comment_content] => This is another comment... 
      [child] => Array 
       (
       ) 

     ) 

    [2] => Array 
     (
      [comment_id] => 6 
      [comment_content] => This is another comment... 
      [child] => Array 
       (
       ) 
     ) 
) 

私の現在の機能は次のようになります。

function RecursiveWrite($array) { 
    foreach ($array as $vals) { 
     echo $vals['comment_content'] . "\n"; 
     RecursiveWrite($vals['child']); 
    } 
} 
+0

を出力しますか? – jb1785

+0

ヘイ・ジェニファー、ここに投稿したコードは問題ありません。バグの発見に役立つコードをいくつか追加する必要があります。コメント7がまったく印刷されていない場合は、おそらく(a)ループに "break"または "return"文があるか、(b)スクリプト全体を終了させるエラーがあります。コードがかなり複雑な場合は、実際には何かになる可能性があります。 –

答えて

0

多分私はあなたの質問を理解していないが、関数は罰金です。あなたが使用しているアレイの例を投稿することができます

私はただ、スペーサーを追加しましたし、あなたの機能

function RecursiveWrite($array, $spacer='') { 
    foreach ($array as $vals) { 
     echo $spacer . $vals['comment_id'] . ' - ' . $vals['comment_content'] . "\n"; 
     RecursiveWrite($vals['child'], $spacer . ' '); 
    } 
} 

にcomment_idそして、それは

1 - This is a comment... 
    3 - This is a reply to the comment... 
    4 - This is a reply to the reply... 
    7 - This is a another reply to the reply... 
2 - This is another comment... 
6 - This is another comment... 
+0

あなたは絶対に正しいです...機能は大丈夫でした...私は間違ったことをしていました。どうもありがとう。 – Jennifer