给你一些信息,你可以更改列名并使用
$data=array(
array('Menu_ID'=>1, 'Menu_Name'=>'Catalog', 'Menu_Link'=>'#', 'Menu_ParentID'=>0),
array('Menu_ID'=>2, 'Menu_Name'=>'Reports', 'Menu_Link'=>'#', 'Menu_ParentID'=>0),
array('Menu_ID'=>3, 'Menu_Name'=>'Products','Menu_Link'=> '#','Menu_ParentID'=> 1),
array('Menu_ID'=>4, 'Menu_Name'=>'Sales','Menu_Link'=> '#', 'Menu_ParentID'=>2),
array('Menu_ID'=>5, 'Menu_Name'=>'Customers','Menu_Link'=> '#', 'Menu_ParentID'=>2),
array('Menu_ID'=>6, 'Menu_Name'=>'Tvs','Menu_Link'=> '#','Menu_ParentID'=> 3));
print_r(loop_menu($data));
// Menu_ID Menu_Name Menu_Link Menu_ParentID
function loop_menu($rows,$parent = 0){
$arr=array();
$i=0;
foreach ($rows as $row)
{
if (array_key_exists('Menu_ParentID',$row) && $row['Menu_ParentID'] == $parent){
if(array_key_exists($i,$arr)){
$arr[$i]=array();
}
$arr[$i]['data']=$row;
$arr[$i]['child']= loop_menu($rows,$row['Menu_ID']);
$i++;
}
}
return $arr;
}
然后
Array
(
[0] => Array
(
[data] => Array
(
[Menu_ID] => 1
[Menu_Name] => Catalog
[Menu_Link] => #
[Menu_ParentID] => 0
)
[child] => Array
(
[0] => Array
(
[data] => Array
(
[Menu_ID] => 3
[Menu_Name] => Products
[Menu_Link] => #
[Menu_ParentID] => 1
)
[child] => Array
(
[0] => Array
(
[data] => Array
(
[Menu_ID] => 6
[Menu_Name] => Tvs
[Menu_Link] => #
[Menu_ParentID] => 3
)
[child] => Array
(
)
)
)
)
)
)
[1] => Array
(
[data] => Array
(
[Menu_ID] => 2
[Menu_Name] => Reports
[Menu_Link] => #
[Menu_ParentID] => 0
)
[child] => Array
(
[0] => Array
(
[data] => Array
(
[Menu_ID] => 4
[Menu_Name] => Sales
[Menu_Link] => #
[Menu_ParentID] => 2
)
[child] => Array
(
)
)
[1] => Array
(
[data] => Array
(
[Menu_ID] => 5
[Menu_Name] => Customers
[Menu_Link] => #
[Menu_ParentID] => 2
)
[child] => Array
(
)
)
)
)
)
然后将数组之类的代码编码为 ul
http://sandbox.onlinephpfunctions.com/code/2b3ab04f959413ebf75b65034edd60da61ed0020
另一种数组样式
$arr[$i]['data'] = $row;
$arr[$i]['child']= loop_menu($rows,$row['Menu_ID']);
改成
$row['child'] = loop_menu($rows,$row['Menu_ID']);
$arr[$i] = $row;