实例
<?php $label = array( ['mid' => 1, 'pid' => 0, 'ord' => 10, 'title' => '用户管理', 'controller' => '', 'method' => '', 'ishidden' => 1, 'status' => 1,], ['mid' => 2, 'pid' => 0, 'ord' => 10, 'title' => '权限管理', 'controller' => '', 'method' => '', 'ishidden' => 1, 'status' => 1,], ['mid' => 3, 'pid' => 0, 'ord' => 10, 'title' => '系统设置', 'controller' => '', 'method' => '', 'ishidden' => 1, 'status' => 1,], ['mid' => 4, 'pid' => 1, 'ord' => 10, 'title' => '管理员列表', 'controller' => 'Admin', 'method' => 'Index', 'ishidden' => 1, 'status' => 1,], ['mid' => 5, 'pid' => 1, 'ord' => 10, 'title' => '管理员添加', 'controller' => 'Admin', 'method' => 'add', 'ishidden' => 1, 'status' => 1,], ['mid' => 6, 'pid' => 1, 'ord' => 10, 'title' => '管理员保存', 'controller' => 'Admin', 'method' => 'save', 'ishidden' => 1, 'status' => 1,], ); // echo '<pre>'; // print_r($label); ?> <!DOCTYPE html> <html> <head> <style type="text/css"> table,tr,th,td{border: 1px solid pink;} </style> </head> <body> <table border="0" cellpadding="0" cellspacing="0"> <tr> <th>菜单id</th> <th>父级id</th> <th>排序</th> <th>名称</th> <th>controller</th> <th>method</th> <th>是否隐藏</th> <th>状态</th> </tr> <?php // $key等于数组中的key值,$val等于key所对应的值 foreach($label as $key=>$val){ if($val['pid']==0){ // 下面为$data变量赋值注意,第二个开始要用.=追加方式来去赋值,否则会被覆盖. $data = '<tr>'; $data .= '<td>'.$val['mid'].'</td>'; $data .= '<td>'.$val['pid'].'</td>'; $data .= '<td>'.$val['ord'].'</td>'; $data .= '<td>'.$val['title'].'</td>'; $data .= '<td>'.$val['controller'].'</td>'; $data .= '<td>'.$val['method'].'</td>'; $data .= '<td>'.$val['ishidden'].'</td>'; $data .= '<td>'.$val['status'].'</td>'; $data .= '</tr>'; print_r($data); } } ?> </table> <hr> <table border="0" cellpadding="0" cellspacing="0"> <tr> <th>菜单id</th> <th>父级id</th> <th>排序</th> <th>名称</th> <th>controller</th> <th>method</th> <th>是否隐藏</th> <th>状态</th> </tr> <?php foreach($label as $key=>$val){ if($val['pid']==1){ $v = '<tr>'; $v .= '<td>'.$val['mid'].'</td>'; $v .= '<td>'.$val['pid'].'</td>'; $v .= '<td>'.$val['ord'].'</td>'; $v .= '<td>'.$val['title'].'</td>'; $v .= '<td>'.$val['controller'].'</td>'; $v .= '<td>'.$val['method'].'</td>'; $v .= '<td>'.$val['ishidden'].'</td>'; $v .= '<td>'.$val['status'].'</td>'; $v .= '</tr>'; print_r($v); } } ?> </table> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例