[英]recursive array menu php
如何建立一個將這個代碼轉換成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;
}
請幫我。
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;
}
我雖然沒有測試此代碼
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.