Php 打印多维数组并设置其样式

Php 打印多维数组并设置其样式,php,codeigniter,multidimensional-array,nested,Php,Codeigniter,Multidimensional Array,Nested,我用这种方法构建了一个数组 Array ( [6] => Array ( [id] => 6 [parent_id] => [text] => Top level comment [level] => 1 ) [1] => Array ( [id] => 1 [parent_id] =>

我用这种方法构建了一个数组

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 ) ) 排列 ( [6] =>阵列 ( [id]=>6 [家长id]=> [文本]=>顶级评论 [级别]=>1 ) [1] =>阵列 ( [id]=>1 [家长id]=> [文本]=>顶级评论 [级别]=>1 [子项]=>数组 ( [2] =>阵列 ( [id]=>2 [parent_id]=>1 [文本]=>对#1的回应 ) [3] =>阵列 ( [id]=>3 [parent_id]=>1 [文本]=>对#1的回应 [子项]=>数组 ( [4] =>阵列 ( [id]=>4 [家长id]=>3 [文本]=>对#3的回应 ) ) ) [10] =>阵列 ( [id]=>10 [parent_id]=>1 [文本]=>对#2的回应 [子项]=>数组 ( [11] =>阵列 ( [id]=>11 [家长id]=>10 [文本]=>对#10的回应 [子项]=>数组 ( [13] =>阵列 ( [id]=>13 [家长id]=>11 [文本]=>对#11的回应 [子项]=>数组 ( [14] =>阵列 ( [id]=>14 [家长id]=>13 [文本]=>对#13的回应 ) ) ) ) ) [12] =>阵列 ( [id]=>12 [家长id]=>10 [文本]=>对#10的回应 ) ) ) ) ) [5] =>阵列 ( [id]=>5 [家长id]=> [文本]=>顶级评论 [级别]=>1 ) [9] =>阵列 ( [id]=>9 [家长id]=> [文本]=>顶级评论 [级别]=>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{“Response to#1”,“childrent”:“Response to{“Response to#”1”,“childrent”:“Response”{“Response”:“Response to{“id”:4”:“parent”{“parent”{“id”:4”,“parent”;“parent”;“parent”;“parent”;““对#2的回应”、“儿童”:{“11”:{“id”:11,“家长id”:10,“文本”:“对#10的回应”、“儿童”:{“13”:{“id”:13,“家长id”:11,“文本”:“对#11的回应”、“儿童”:{“14”:{“id”:14,“家长id”:13,“文本”:“对#13}}}}的回应”,12”:{“id”:12,“家长id”:10,“文本”:“对{“家长id:10的回应”,“文本”:“对}10的回应”,“5}”,最高级别:{“文本”:“注释”:1”:“家长id”:“:{“id”:9,“parent_id”:null,“text”:“Top-level comment”,“level”:1}” 什么是最简单的方法来回应它和风格?像雷迪特一样说。我想在视图文件中进行样式设置。

您可以尝试使用,但我更愿意使用自定义函数,该函数在数组中循环并生成HTML

编辑:

函数绘制注释($comments,$level=0) { echo'
    ”; foreach($comments作为$comment) { 绘制注释($comment); 如果(!empty($comment['children'])) 绘制注释($comment['children',$level+1); } 回声“
”; } 函数draw_comment($comment) { 回音“
  • ”; echo$comment['text']; 回音“
  • ”; } 绘制注释($comments\u数组);
    您想与谁分享如何循环使用该阵列?我不知道怎么做,因为孩子的数量=x。 {"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}}
    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);