我想将每个子数组包装在<ul>
中,但我不完全确定它是如何完成的?
function RecursiveWrite($array) {
foreach ($array as $v) {
echo '<li>';
echo $v['id'];
if(isset($v['children']))
RecursiveWrite($v['children']);
echo '</li>';
}
}
echo '<ul>';
RecursiveWrite($comments);
echo '</ul>';
输出:
<ul>
<li>81
<li>173</li>
<li>84
<li>85
<li>131
<li>176</li>
</li>
</li>
<li>174</li>
<li>175</li>
</li>
</li>
<li>178</li>
</ul>
我想得到的输出:
<ul>
<li>81
<ul>
<li>173</li>
<li>84
<ul>
<li>85
<ul>
<li>131
<ul>
<li>176</li>
</ul>
</li>
</ul>
</li>
<li>174</li>
<li>175</li>
</ul>
</li>
</ul>
</li>
<li>178</li>
</ul>
原始阵列:
array (
0 =>
array (
'id' => '81',
'parent' => NULL,
'children' =>
array (
0 =>
array (
'id' => '173',
'parent' => '81',
),
1 =>
array (
'id' => '84',
'parent' => '81',
'children' =>
array (
0 =>
array (
'id' => '85',
'parent' => '84',
'children' =>
array (
0 =>
array (
'id' => '131',
'parent' => '85',
'children' =>
array (
0 =>
array (
'id' => '176',
'parent' => '131',
),
),
),
),
),
1 =>
array (
'id' => '174',
'parent' => '84',
),
2 =>
array (
'id' => '175',
'parent' => '84',
),
),
),
),
),
1 =>
array (
'id' => '178',
'parent' => NULL,
),
)
答案 0 :(得分:2)
看起来你只需要在foreach循环之前和之后回显<ul>
标签。
简单如......
function RecursiveWrite($array) {
echo "<ul>";
foreach ($array as $v) {
...
}
echo "</ul>";
}
RecursiveWrite($comments);