【发布时间】:2013-09-06 20:20:05
【问题描述】:
我有一个多维数组和两个用于解析抛出数组的函数。我想接收多级无序列表。我的 html 标签有错误,但我找不到。 数组是:
Array
(
[2] => Array
(
[id] => 2
[parent_id] => 0
[name] => task2
[childs] => Array
(
[1] => Array
(
[id] => 1
[parent_id] => 2
[name] => task1
)
)
)
[3] => Array
(
[id] => 3
[parent_id] => 0
[name] => task3
[childs] => Array
(
[4] => Array
(
[id] => 4
[parent_id] => 3
[name] => task4
)
[5] => Array
(
[id] => 5
[parent_id] => 3
[name] => task5
[childs] => Array
(
[6] => Array
(
[id] => 6
[parent_id] => 5
[name] => task6
)
)
)
)
)
)
正确的函数是:
function formatHtmlARC11($array) {
foreach ($array as $k => $v) {
if (is_array($v['childs']) && !empty($v['childs'])) {
echo $v['id'];
$sub=$this->formatHtmlARC11($v['childs']);
} else {
echo $v['id'];
}
}
return $var;
}
我的formatHtml函数有问题的是:
function formatHtmlARC($array,$bul) {
$htmlcode .='<ul>';
if($bul==true){
$htmlcode .='</ul>';
$bul=false;
}
foreach ($array as $k => $v) {
if (is_array($v['childs']) && !empty($v['childs'])) {
$htmlcode .='<li>';
$htmlcode .= $v['id'];
$htmlcode .='</li>';
$bul=true;
$sub=$this->formatHtmlARC($v['childs'], $bul);
} else {
$htmlcode .='<li>';
$htmlcode .= $v['id'];
$htmlcode .='</li>';
}
$htmlcode .='</ul>';
}
return $htmlcode;
}
【问题讨论】:
-
可以添加输出的 HTML 吗?
-
我没有问题函数的结果。从正确的函数结果是:
213456.
标签: php html multidimensional-array