2013-05-19 97 views
0

對於我正在開發的wordpress網站,我正在製作一個動態菜單,用戶可以使用管理菜單進行製作。陷入這只是我的問題最少。修改PHP集合

代碼我使用返回這些陣列:

     Array 
(
    [0] => WP_Post Object 
     (
      [ID] => 35 
      [post_author] => 1 
      [post_date] => 2013-05-19 15:46:22 
      [post_date_gmt] => 2013-05-19 15:46:22 
      [post_content] => 
      [post_title] => 
      [post_excerpt] => 
      [post_status] => publish 
      [comment_status] => open 
      [ping_status] => open 
      [post_password] => 
      [post_name] => 35 
      [to_ping] => 
      [pinged] => 
      [post_modified] => 2013-05-19 16:07:09 
      [post_modified_gmt] => 2013-05-19 16:07:09 
      [post_content_filtered] => 
      [post_parent] => 0 
      [guid] => http://adapt.local/?p=35 
      [menu_order] => 1 
      [post_type] => nav_menu_item 
      [post_mime_type] => 
      [comment_count] => 0 
      [filter] => raw 
      [db_id] => 35 
      [menu_item_parent] => 0 
      [object_id] => 32 
      [object] => training 
      [type] => post_type 
      [type_label] => Training 
      [url] => http://adapt.local/training/alcohol/ 
      [title] => Alcohol 
      [target] => 
      [attr_title] => 
      [description] => 
      [classes] => Array 
       (
        [0] => 
       ) 

      [xfn] => 
     ) 

    [1] => WP_Post Object 
     (
      [ID] => 36 
      [post_author] => 1 
      [post_date] => 2013-05-19 16:07:09 
      [post_date_gmt] => 2013-05-19 16:07:09 
      [post_content] => 
      [post_title] => 
      [post_excerpt] => 
      [post_status] => publish 
      [comment_status] => open 
      [ping_status] => open 
      [post_password] => 
      [post_name] => 36 
      [to_ping] => 
      [pinged] => 
      [post_modified] => 2013-05-19 16:07:09 
      [post_modified_gmt] => 2013-05-19 16:07:09 
      [post_content_filtered] => 
      [post_parent] => 0 
      [guid] => http://adapt.local/?p=36 
      [menu_order] => 2 
      [post_type] => nav_menu_item 
      [post_mime_type] => 
      [comment_count] => 0 
      [filter] => raw 
      [db_id] => 36 
      [menu_item_parent] => 35 
      [object_id] => 32 
      [object] => training 
      [type] => post_type 
      [type_label] => Training 
      [url] => http://adapt.local/training/alcohol/ 
      [title] => Alcohol 
      [target] => 
      [attr_title] => 
      [description] => 
      [classes] => Array 
       (
        [0] => 
       ) 

      [xfn] => 
     ) 

) 

爲了解釋:如果menu_item_parent = 0,這意味着它是頂節點,並且如果menu_item_parent> 0,這意味着它是一個'子節點」。

我想這個可怕的數組轉換到更有用的東西,最好是這樣的

Array 
(
    [35] => Array 
    (
     name => "Topnode" 
     url => "http://topnodeurl" 
     items => Array 
     (
      name => "Subnode" 
      url => "http://subnodeurl" 
     ) 
    ) 
) 

心想:嘿,這可能不是那麼難。但是,顯然,這似乎並沒有工作:

foreach($menuitems as $menuitem) { 
    if(!$menuitem->menu_item_parent) { 
     $items[$menuitem->ID] = array("name" => $menuitem->title,"items" => array()); 
     #print_r($items); 
    } 
    else { 
     $parent = $items[$menuitem->menu_item_parent]['items']; 
     $parent = array("name" => $menuitem->title, "url" => $menuitem->url); 
    } 
} 

任何想法?

回答

2

做一個遞歸函數

function make_menu($items, $parent_id = 0) 
{ 
    $menu = []; 
    foreach($items as $key => $item) { 
     if (item['menu_item_parent'] == $parent_id) { 
      $item['childs'] = make_menu($items, $item['ID']); 
      $menu[] = $item; 

      // Helps speed up the foreach by removing items that are not needed any more 
      unset($item[$key]); 
     } 
    } 

    return $menu; 
} 

這樣你可以有那麼一個更深層次的功能只有1層

+0

如何將這項工作?由於遞歸函數沒有返回任何東西,我不相信$ items ['childs']會做些什麼? –

+0

抱歉,我的壞話忘了返回菜單 – MKroeders

+0

完美!這似乎是做這項工作。我現在不應該擔心所有'額外數據'。這只是做一切。 –