2013-03-02 72 views
0

我这样打印多维数组和风格它

 
Array 
(
    [6] => Array 
     (
      [id] => 6 
      [parent_id] => 
      [text] => Top level comment 
      [level] => 1 
     ) 

    [1] => Array 
     (
      [id] => 1 
      [parent_id] => 
      [text] => Top level comment 
      [level] => 1 
      [children] => Array 
       (
        [2] => Array 
         (
          [id] => 2 
          [parent_id] => 1 
          [text] => Response to #1 
         ) 

        [3] => Array 
         (
          [id] => 3 
          [parent_id] => 1 
          [text] => Response to #1 
          [children] => Array 
           (
            [4] => Array 
             (
              [id] => 4 
              [parent_id] => 3 
              [text] => Response to #3 
             ) 

           ) 

         ) 

        [10] => Array 
         (
          [id] => 10 
          [parent_id] => 1 
          [text] => Response to #2 
          [children] => Array 
           (
            [11] => Array 
             (
              [id] => 11 
              [parent_id] => 10 
              [text] => Response to #10 
              [children] => Array 
               (
                [13] => Array 
                 (
                  [id] => 13 
                  [parent_id] => 11 
                  [text] => Response to #11 
                  [children] => Array 
                   (
                    [14] => Array 
                     (
                      [id] => 14 
                      [parent_id] => 13 
                      [text] => Response to #13 
                     ) 

                   ) 

                 ) 

               ) 

             ) 

            [12] => Array 
             (
              [id] => 12 
              [parent_id] => 10 
              [text] => Response to #10 
             ) 

           ) 

         ) 

       ) 

     ) 

    [5] => Array 
     (
      [id] => 5 
      [parent_id] => 
      [text] => Top level comment 
      [level] => 1 
     ) 

    [9] => Array 
     (
      [id] => 9 
      [parent_id] => 
      [text] => Top level comment 
      [level] => 1 
     ) 

) 

JSON

{"6":{"id":6,"parent_id":null,"text":"Top level comment","level":1},"1":{"id":1,"parent_id":null,"text":"Top level comment","level":1,"children":{"2":{"id":2,"parent_id":1,"text":"Response to #1"},"3":{"id":3,"parent_id":1,"text":"Response to #1","children":{"4":{"id":4,"parent_id":3,"text":"Response to #3"}}},"10":{"id":10,"parent_id":1,"text":"Response to #2","children":{"11":{"id":11,"parent_id":10,"text":"Response to #10","children":{"13":{"id":13,"parent_id":11,"text":"Response to #11","children":{"14":{"id":14,"parent_id":13,"text":"Response to #13"}}}}},"12":{"id":12,"parent_id":10,"text":"Response to #10"}}}}},"5":{"id":5,"parent_id":null,"text":"Top level comment","level":1},"9":{"id":9,"parent_id":null,"text":"Top level comment","level":1}}

什么是呼应它的最简单的方法和作风它阵列建立?说像reddit。我想在视图文件中做样式。

回答

0

你可以试试http://ellislab.com/codeigniter/user-guide/helpers/html_helper.html#ol_and_ul,但我宁愿去自定义函数循环访问数组并生成HTML。

编辑:

function draw_comments($comments, $level = 0) 
{ 
    echo '<ul class="level' . $level . '">'; 
    foreach($comments as $comment) 
    { 
     draw_comment($comment); 

     if(!empty($comment['children'])) 
      draw_comments($comment['children'], $level+1); 
    } 
    echo '</ul>'; 
} 

function draw_comment($comment) 
{ 
    echo '<li>'; 
    echo $comment['text']; 
    echo '</li>'; 
} 

draw_comments($comments_array); 
+0

您对子级想分享您如何通过阵列倒是循环?我不知道该怎么做,因为孩子的数量= x。 – Martin 2013-03-02 11:57:53