I have an multidimensional array and two functions for parsing throw the array. I want to receive multilevel unordered list. I have a mistake with html tags, but I can't find. Array is:
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
)
)
)
)
)
)
The correct function is:
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;
}
My formatHtml function with a problem is:
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;
}