我已经这样构建了一个数组
<>之前数组([6] =>数组([id] => 6[parent_id] =>[text] =>顶级注释[level] => 1)[1] =>数组([id] => 1[parent_id] =>[text] =>顶级注释[level] => 1[children] =>数组([2] =>数组([id] => 2[parent_id] => 1[text] =>对#1的回应)[3] =>阵列([id] => 3[parent_id] => 1[text] =>对#1的回应[children] =>数组([4] =>数组([id] => 4[parent_id] => 3[text] =>对#3的回应)))[10] =>数组([id] => 10[parent_id] => 1[text] =>对#2的回应[children] =>数组([11] =>数组([id] => 11[parent_id] => 10[text] =>对#10的回应[children] =>数组([13] =>数组([id] => 13[parent_id] => 11[text] =>对#11的回应[children] =>数组([14] =>数组([id] => 14[parent_id] => 13[text] =>对#13的回应)))))[12] =>数组([id] => 12[parent_id] => 10[text] =>对#10的回应)))))[5] =>数组([id] => 5[parent_id] =>[text] =>顶级注释[level] => 1)[9] =>数组([id] => 9[parent_id] =>[text] =>顶级注释[level] => 1))之前JSON{" 6 ": {" id ": 6"parent_id":空,"文本":"顶级评论","水平":1}," 1 ":{" id ": 1、"parent_id":空,"文本":"顶级评论","水平":1、"孩子":{" 2 ":{" id ": 2,"parent_id":1、"文本":"应对# 1"},"3":{" id ": 3,"parent_id":1、"文本":"应对# 1","孩子们":{" 4 ":{" id ": 4,"parent_id":3,"文本":"应对# 3"}}},"10":{" id ": 10"parent_id":1、"文本":"对# 2","孩子们":{" 11 ":{" id ": 11日"parent_id":10"文本":"应对# 10","孩子们":{" 13 ":{" id ": 13日"parent_id":11日,"文本":"回应# 11"、"孩子":{"14":{" id ": 14日"parent_id":13日,"文本":"应对# 13}}}}},"12":{" id ": 12,"parent_id":10"文本":"应对# 10}}}}},"5":{" id ": 5"parent_id":空,"文本":"顶级评论","水平":1},"9":{" id ": 9,"parent_id":空,"文本":"顶级评论","水平":1}}之前最简单的方法是什么?比如喜欢reddit。我想在视图文件中做样式
您可以尝试使用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);