Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $data=array(
- array('id'=>1,'url'=>'welcome/index','title'=>'Home','parent_id'=>0),
- array('id'=>2,'url'=>'welcome/about','title'=>'About','parent_id'=>0),
- array('id'=>3,'url'=>'#','title'=>'Product','parent_id'=>0),
- array('id'=>4,'url'=>'welcome/productlist1','title'=>'Product List 1','parent_id'=>3),
- array('id'=>5,'url'=>'welcome/productlist1','title'=>'Product List 1','parent_id'=>4),
- array('id'=>6,'url'=>'welcome/productlist1','title'=>'Product List 1','parent_id'=>5),
- array('id'=>7,'url'=>'welcome/productlist2','title'=>'Product List 2','parent_id'=>5),
- array('id'=>8,'url'=>'welcome/productlist3','title'=>'Product List 3','parent_id'=>5),
- array('id'=>9,'url'=>'welcome/productlist4','title'=>'Product List 4','parent_id'=>5),
- array('id'=>10,'url'=>'welcome/productlist5','title'=>'Product List 5','parent_id'=>5),
- array('id'=>11,'url'=>'welcome/productlist6','title'=>'Product List 6','parent_id'=>5),
- array('id'=>12,'url'=>'welcome/productlist7','title'=>'Product List 7','parent_id'=>5),
- array('id'=>13,'url'=>'welcome/productlist2','title'=>'Product List 2','parent_id'=>4),
- array('id'=>14,'url'=>'welcome/productlist3','title'=>'Product List 3','parent_id'=>4),
- array('id'=>15,'url'=>'welcome/productlist4','title'=>'Product List 4','parent_id'=>4),
- array('id'=>16,'url'=>'welcome/productlist5','title'=>'Product List 5','parent_id'=>4),
- array('id'=>17,'url'=>'welcome/productlist6','title'=>'Product List 6','parent_id'=>4),
- array('id'=>18,'url'=>'welcome/productlist7','title'=>'Product List 7','parent_id'=>4),
- array('id'=>19,'url'=>'welcome/productlist2','title'=>'Product List 2','parent_id'=>3),
- array('id'=>20,'url'=>'welcome/productlist1','title'=>'Product List 1','parent_id'=>19),
- array('id'=>21,'url'=>'welcome/productlist2','title'=>'Product List 2','parent_id'=>19),
- array('id'=>22,'url'=>'welcome/productlist7','title'=>'Product List 7','parent_id'=>19),
- array('id'=>23,'url'=>'welcome/productlist3','title'=>'Product List 3','parent_id'=>3),
- array('id'=>24,'url'=>'welcome/productlist4','title'=>'Product List 4','parent_id'=>3),
- array('id'=>25,'url'=>'welcome/productlist1','title'=>'Product List 1','parent_id'=>24),
- array('id'=>26,'url'=>'welcome/productlist2','title'=>'Product List 2','parent_id'=>24),
- array('id'=>27,'url'=>'welcome/productlist7','title'=>'Product List 7','parent_id'=>24),
- array('id'=>28,'url'=>'welcome/productlist5','title'=>'Product List 5','parent_id'=>3),
- array('id'=>29,'url'=>'welcome/productlist6','title'=>'Product List 6','parent_id'=>3),
- array('id'=>30,'url'=>'welcome/productlist7','title'=>'Product List 7','parent_id'=>3),
- array('id'=>31,'url'=>'welcome/about','title'=>'Article','parent_id'=>0),
- array('id'=>32,'url'=>'welcome/contact','title'=>'Contact','parent_id'=>0)
- );
- function GenerateDataArray($arr, $parent = 0, $level=0)
- {
- $pages = Array();
- foreach($arr as $page)
- {
- if($page['parent_id'] == $parent)
- {
- $page['level']= isset($page['sub'])? $level+1:$level;
- $page['sub'] = isset($page['sub']) ? $page['sub'] : GenerateDataArray($arr, $page['id'], $level+1);
- $pages[] = $page;
- }
- }
- return $pages;
- }
- echo array_reduce(
- $menu = GenerateDataArray($data), $cb = function($m, $i) use (&$cb) {
- $m .= "<li><a href='{$i['url']}.php'>{$i['title']}</a>";
- if (isset($i['sub']) && !empty($i['sub']))
- $m .= array_reduce($i['sub'], $cb, "<ul>") . "</ul>";
- $m .= "</li>";
- return $m;
- }, "<ul>"
- ) . "</ul>";
Add Comment
Please, Sign In to add comment