cdsatrian

tree menu

Sep 7th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.50 KB | None | 0 0
  1. <?php
  2. $data=array(
  3.     array('id'=>1,'url'=>'welcome/index','title'=>'Home','parent_id'=>0),
  4.     array('id'=>2,'url'=>'welcome/about','title'=>'About','parent_id'=>0),
  5.     array('id'=>3,'url'=>'#','title'=>'Product','parent_id'=>0),
  6.     array('id'=>4,'url'=>'welcome/productlist1','title'=>'Product List 1','parent_id'=>3),
  7.     array('id'=>5,'url'=>'welcome/productlist1','title'=>'Product List 1','parent_id'=>4),
  8.     array('id'=>6,'url'=>'welcome/productlist1','title'=>'Product List 1','parent_id'=>5),
  9.     array('id'=>7,'url'=>'welcome/productlist2','title'=>'Product List 2','parent_id'=>5),
  10.     array('id'=>8,'url'=>'welcome/productlist3','title'=>'Product List 3','parent_id'=>5),
  11.     array('id'=>9,'url'=>'welcome/productlist4','title'=>'Product List 4','parent_id'=>5),
  12.     array('id'=>10,'url'=>'welcome/productlist5','title'=>'Product List 5','parent_id'=>5),
  13.     array('id'=>11,'url'=>'welcome/productlist6','title'=>'Product List 6','parent_id'=>5),
  14.     array('id'=>12,'url'=>'welcome/productlist7','title'=>'Product List 7','parent_id'=>5),
  15.     array('id'=>13,'url'=>'welcome/productlist2','title'=>'Product List 2','parent_id'=>4),
  16.     array('id'=>14,'url'=>'welcome/productlist3','title'=>'Product List 3','parent_id'=>4),
  17.     array('id'=>15,'url'=>'welcome/productlist4','title'=>'Product List 4','parent_id'=>4),
  18.     array('id'=>16,'url'=>'welcome/productlist5','title'=>'Product List 5','parent_id'=>4),
  19.     array('id'=>17,'url'=>'welcome/productlist6','title'=>'Product List 6','parent_id'=>4),
  20.     array('id'=>18,'url'=>'welcome/productlist7','title'=>'Product List 7','parent_id'=>4),
  21.     array('id'=>19,'url'=>'welcome/productlist2','title'=>'Product List 2','parent_id'=>3),
  22.     array('id'=>20,'url'=>'welcome/productlist1','title'=>'Product List 1','parent_id'=>19),
  23.     array('id'=>21,'url'=>'welcome/productlist2','title'=>'Product List 2','parent_id'=>19),
  24.     array('id'=>22,'url'=>'welcome/productlist7','title'=>'Product List 7','parent_id'=>19),
  25.     array('id'=>23,'url'=>'welcome/productlist3','title'=>'Product List 3','parent_id'=>3),
  26.     array('id'=>24,'url'=>'welcome/productlist4','title'=>'Product List 4','parent_id'=>3),
  27.     array('id'=>25,'url'=>'welcome/productlist1','title'=>'Product List 1','parent_id'=>24),
  28.     array('id'=>26,'url'=>'welcome/productlist2','title'=>'Product List 2','parent_id'=>24),
  29.     array('id'=>27,'url'=>'welcome/productlist7','title'=>'Product List 7','parent_id'=>24),
  30.     array('id'=>28,'url'=>'welcome/productlist5','title'=>'Product List 5','parent_id'=>3),
  31.     array('id'=>29,'url'=>'welcome/productlist6','title'=>'Product List 6','parent_id'=>3),
  32.     array('id'=>30,'url'=>'welcome/productlist7','title'=>'Product List 7','parent_id'=>3),
  33.     array('id'=>31,'url'=>'welcome/about','title'=>'Article','parent_id'=>0),
  34.     array('id'=>32,'url'=>'welcome/contact','title'=>'Contact','parent_id'=>0)
  35. );
  36.  
  37. function GenerateDataArray($arr, $parent = 0, $level=0)
  38. {
  39.     $pages = Array();
  40.     foreach($arr as $page)
  41.     {
  42.         if($page['parent_id'] == $parent)
  43.         {
  44.             $page['level']= isset($page['sub'])? $level+1:$level;
  45.             $page['sub'] = isset($page['sub']) ? $page['sub'] : GenerateDataArray($arr, $page['id'], $level+1);
  46.             $pages[] = $page;
  47.         }
  48.     }
  49.     return $pages;
  50. }
  51.  
  52. echo array_reduce(
  53.    $menu = GenerateDataArray($data), $cb = function($m, $i) use (&$cb) {
  54.       $m .= "<li><a href='{$i['url']}.php'>{$i['title']}</a>";
  55.       if (isset($i['sub']) && !empty($i['sub']))
  56.          $m .= array_reduce($i['sub'], $cb, "<ul>") . "</ul>";
  57.       $m .= "</li>";
  58.       return $m;
  59.    }, "<ul>"
  60. ) . "</ul>";
Add Comment
Please, Sign In to add comment