如何在此PHP数组中进入嵌套子级的第三级?

时间:2019-04-30 04:39:44

标签: php wordpress

我建立了一个简单的PHP函数,该函数检索Wordpress菜单并将其转换为可读数组。这是我的代码:

function wp_get_menu_array($current_menu) {

$array_menu = wp_get_nav_menu_items($current_menu);
$menu = array();
foreach ($array_menu as $m) {
    if (empty($m->menu_item_parent)) {
        $menu[$m->ID] = array();
        $menu[$m->ID]['ID']      =   $m->ID;
        $menu[$m->ID]['title']       =   $m->title;
        $menu[$m->ID]['url']         =   $m->url;
        $menu[$m->ID]['children']    =   array();
    }
}
$submenu = array();
foreach ($array_menu as $m) {
    if ($m->menu_item_parent) {
        $submenu[$m->ID] = array();
        $submenu[$m->ID]['ID']       =   $m->ID;
        $submenu[$m->ID]['title']    =   $m->title;
        $submenu[$m->ID]['url']  =   $m->url;
        $menu[$m->menu_item_parent]['children'][$m->ID] = $submenu[$m->ID];
    }
}
return $menu;
}

我想要实现的是进一步的(#submenu2),该迭代将遍历$ submenu,检查其中的任何一个是否为后代父代,并将其子代插入数组中。

换句话说:此数组当前创建如下关系:

(如果父母没有孩子,请分配这些值) (如果父母有孩子,请遍历孩子并添加这些值) 我要添加 (如果孩子有孩子,请遍历孩子并添加这些值)。

关于第三个循环的去向/方式的任何指针,我将不胜感激。

提前谢谢!

3 个答案:

答案 0 :(得分:0)

真的需要很多代码吗?

您可以检查以下代码-

wp_get_nav_menu_items( 'Your_Manu_ID' )

答案 1 :(得分:0)

使用以下经过稍微修改的递归解决方案here已解决了从平面数组构建树的问题:

function buildTree( array &$elements, $parentId = 0 )
{
 $branch = array();
 foreach ( $elements as &$element )
  {
    if ( $element->menu_item_parent == $parentId )
    {
        $children = buildTree( $elements, $element->ID );
        if ( $children )
            $element->wpse_children = $children;

        $branch[$element->ID] = $element;
        unset( $element );
    }
 }
 return $branch;
}


$array_menu = wp_get_nav_menu_items('main-menu'); //put the menu slug here
$menu = buildTree( $array_menu );
echo '<pre>';
print_r($menu);

答案 2 :(得分:0)

为我工作:

<nav>
                            <ul>
                                <?php $itens_menu = wp_get_menu_array("principal"); ?> 
                                <?php 
                                     $menu_corrente = 0;
                                     foreach($itens_menu as $menu){
                                           
                                           if($menu_corrente==0) $classe = "todos-os-produtos";
                                           if($menu_corrente==1) $classe = "home";
                                           if($menu_corrente>1) $classe = "";

                                               if(count($menu["children"])==0):
                                    ?>
                                                <li class="sou-categoria-<?php echo $menu["ID"] ?>"><a class="<?php echo $classe; ?>" href="<?php echo $menu["url"]; ?>" title=""><?php echo $menu["title"]; ?></a></li>
                                    <?php
                                            else:
                                    ?>
                                                 <li class="sou-categoria-<?php echo $menu["ID"] ?>"><a class="<?php echo $classe; ?>" href="<?php echo $menu["url"]; ?>" title=""><?php echo $menu["title"]; ?></a>
                                                      <ul>
                                                         <?php 
                                                           foreach($menu["children"] as $submenu){
                                                         ?>
                                                            <li><a href="<?php echo $submenu["url"]; ?>" title="<?php echo $submenu["title"]; ?>"><?php echo $submenu["title"]; ?></a>
                                                              <?php echo terceiro_nivel("principal",$submenu["ID"]); ?>
                                                            </li>
                                                         <?php 
                                                            }
                                                         ?>
                                                     </ul>
                                                 </li>
                                    <?php
                                               endif;

                                               $menu_corrente++;

                                            }  
                                    ?>

                                <li>
                                    <a href="javascript:void(0)" onclick="toogleBuscaHeaderFixo()" title="Faça uma pesquisa" class="btn btn-primary"><i class="fa fa-search"></i></a>
                                </li>
                                <li>
                                    <a href="<?php echo get_option('home'); ?>/carrinho" title="Meu Carrinho" class="btn btn-primary">
                                        <i class="fa fa-shopping-cart " aria-hidden="true"></i> Carrinho: <span>
                                          <?php
                           
                                             global $woocommerce;
                                             echo $woocommerce->cart->get_cart_total();

                                          ?>
                                        </span>
                                    </a>
                                </li>  

                            </ul>
                        </nav>

在functions.php中:

function wp_get_menu_array($current_menu) {
 
    $array_menu = wp_get_nav_menu_items($current_menu);
    $menu = array();

    foreach ($array_menu as $m) {
        if (empty($m->menu_item_parent)) {
           
            $menu[$m->ID] = array();
            $menu[$m->ID]['ID']      =   $m->ID;
            $menu[$m->ID]['title']       =   $m->title;
            $menu[$m->ID]['class']       =   $m->class;
            $menu[$m->ID]['url']         =   $m->url;
            $menu[$m->ID]['children']    =   array();
        }
        

    }
    $submenu = array();
    foreach ($array_menu as $m) {
        if ($m->menu_item_parent) {
            $submenu[$m->ID] = array();
            $submenu[$m->ID]['ID']       =   $m->ID;
            $submenu[$m->ID]['title']    =   $m->title;
            $submenu[$m->ID]['class']       =   $m->class;
            $submenu[$m->ID]['url']  =   $m->url;
            $menu[$m->menu_item_parent]['children'][$m->ID] = $submenu[$m->ID];
        }
    }
    return $menu;
     
}

// HABILITAR O TERCEIRO NÍVEL DE SUBMENUS AO MENU PRINCIPAL
function terceiro_nivel($current_menu,$idMenu){

    $array_menu = wp_get_nav_menu_items($current_menu);
    $menu = array();
    
    $i = 0;
    $saida = "";

    foreach ($array_menu as $m) {

        if ($m->menu_item_parent == $idMenu) {

           if($i==0){
               
              $saida = $saida.'
                   
                   <ul class="terceiro-nivel"> 
                       <div>

              ';

              $i++;

           }
           
           $saida = $saida.'<li class="valide-'.$m->menu_item_parent.'"><a href="'.$m->url.'" title="'.$m->title.'">'.$m->title.'</a></li>';

        }

        
    }
    
    if($i!=0){
    
       $saida = $saida.'   </div>
                    </ul>';

    }

    return $saida;

}