How to build a php function that will convert this code into html menu this is the post that i make to the function via ajax
list[0][id] 55
list[1][id] 69
list[2][children][0][id] 67
list[2][children][1][id] 68
list[2][id] 57
And here's the array that i get into the function
Array
(
[0] => Array
(
[id] => 55
[children] => Array
(
[0] => Array
(
[id] => 67
[children] => Array
(
[0] => Array
(
[id] => 57
[children] => Array
(
[0] => Array
(
[id] => 68
)
)
)
[1] => Array
(
[id] => 69
)
)
)
)
)
)
i've tryed this function but it doesn't work
function tomenu($arr){
$html = '<ul>'.PHP_EOL;
foreach ($arr as $v){
$html .= '<li>' . $v['id'];
if (array_key_exists('children', $v)){
$html .= $this->tomenu($v['children']);
}
$html .= '</li>'.PHP_EOL;
}
$html .= '</ul>'.PHP_EOL;
return $html;
}
Please help me.
function tomenu($arr) {
$this->html .= '<ul>' . PHP_EOL;
foreach ($arr as $v) {
$this->html .= '<li>' . $v['id'];
if (is_array($v['children'])) {
$this->html .= $this->tomenu($v['children']);
}
$this->html .= '</li>' . PHP_EOL;
}
$this->html .= '</ul>' . PHP_EOL;
$returnVal = $this->html;
$this->html = '';
return $returnVal;
}
i have not tested this code though
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.