【发布时间】:2015-06-14 02:47:41
【问题描述】:
我正在尝试使用 mysql 创建多级下拉菜单作为存储数据,并且我使用 twig 作为主题引擎,我知道外面有大量代码但是,所有这些都是 HTML 作为输出,因为我使用了 twig,所以我需要数组作为输出,然后让 twig 渲染它,(或者可能有其他选项,如果有,请告诉我)。
如果我使用 html 作为输出,则此代码有效。但如果我将数组更改为输出,问题是第二级菜单仅显示 1 个数组或第一个数组,它不会循环。
这是我从 mysql 查询中输出的数组,
Array
(
[0] => Array
(
[id] => 1
[title] => Dashboard
[link] => 1.html
[parent_id] => 0
)
[1] => Array
(
[id] => 2
[title] => Master Data
[link] => 2.html
[parent_id] => 0
)
[2] => Array
(
[id] => 3
[title] => submaster
[link] => 11.html
[parent_id] => 2
)
[3] => Array
(
[id] => 4
[title] => submaster
[link] => 12.html
[parent_id] => 2
)
这是我尝试转换的代码,我的意思是在将 html 编码为输出之前 (ul>li>ul>li>/li>/ul>/li>/lu)
$id = '';
function sub($items, $id){
foreach($items as $item){
if($item['parent_id'] == $id){
return array("link" =>$item['link'],"title"=>$item['title']);
sub($items, $item['id']);
}
}
}
foreach($allmenu as $data){
if($data['parent_id'] == 0){
$id = $data['id'];
$utama[]= array("link"=>$data['link'],"title"=>$data['title'],"sub"=>sub($allmenu, $id));
}
}
这是上面代码的输出
Array
(
[links] => Array
(
[0] => Array
(
[link] => 1.html
[title] => Dashboard
[sub] =>
)
[1] => Array
(
[link] => 2.html
[title] => Master Data
[sub] => Array
(
[link] => 11.html
[title] => Kampus
)
)
)
)
见..在第二个数组(主数据)中应该是两个数组对吗?带有链接 11.html 和 12.html。
如果我的代码有错误,请告诉我,如果有链接可以完成这项工作,请告诉我。谢谢
【问题讨论】:
标签: php mysql arrays multidimensional-array twig