如何构建一个将此代码转换为html菜单的php函数 这是我通过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
这是我进入函数的数组
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
)
)
)
)
)
)
我试过这个功能,但它不起作用
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;
}
请帮帮我。
答案 0 :(得分:-1)
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;
}
我还没有测试过这段代码