我有一张这样的桌子 表名:菜单
Id menu submenu childmenu
1. Oil. Edible. Coconut oil
2. Oil. Edible. Sunflowe oil
3. Oil. Non edible. Deepa oil
4. Oil non edible. Coconut oil
我的预期输出是
Oil
Edible
Coconut oil
Sunflower oil
Non edible
Deepa oil
Coconut oil
如何创建这样的菜单。谢谢你的回答
答案 0 :(得分:0)
这应该让你开始。如果您已阅读文档(您应该阅读),其余部分则不言自明。
$array = array(
array(
'id' => '1',
'menu' => 'Oil',
'submenu' => 'editable',
'childmenu' => 'coco oil'
),
array(
'id' => '2',
'menu' => 'Oil',
'submenu' => 'editable',
'childmenu' => 'sunflower'
),
array(
'id' => '3',
'menu' => 'Oil2',
'submenu' => 'noneditable',
'childmenu' => 'deepa'
),
);
// CONTROLLER
$_data = array();
// $array will come from model result_array()
foreach ($array as $item) {
$_data[$item['menu']][$item['submenu']][] = $item['childmenu'];
}
// send $_data to view
// VIEW
foreach ($_data as $menu => $submenu) {
echo '<i>' . $menu . '</i><br>';
foreach ($submenu as $name => $childmenu) {
echo '<strong>' . $name . '</strong><br>';
foreach ($childmenu as $item) {
echo $item . '<br>';
}
}
}
基本上我重新排列数组以便更友好地使用。这样就可以防止我们以后在视图中检查foreach中的特定子菜单。