简体   繁体   English

如何在php中循环到第三级维数组?

[英]How to loop to a third level dimensional array in php?

Firstly, I want to loop and display the categories dynamically. 首先,我想动态循环显示类别。 It's Parent Category > Child Category > Inner Child Category . 它是父Category > Child Category > Inner Child Category I was able to loop categories till Child Category only. 我能够循环分类直到仅儿童分类。 Tried to loop to the third level(inner child category) but failed miserably. 试图循环到第三级(内部子类别),但失败了。

I was able to loop till the child category however when I tried to access the inner child category. 但是,当我尝试访问内部子类别时,可以循环到子类别。 It loops instance of the inner category to all child category. 它将内部类别的实例循环到所有子类别。 It should only loop accordingly to it's category not all. 它应该仅根据其类别而不是全部循环。

Here is my code. 这是我的代码。 Please do tell me where I did wrong 请告诉我我做错了什么


    $taxonomies = array( 
         'taxonomy' => 'product_cat'

    );
    $args = array(
        'number' => 8,
        'parent' => 0,
        'hide_empty' => FALSE,
        'exclude' => array( 16 )
    );

    $parent_product_categories = get_terms($taxonomies,$args);

    $html .= '<div class="sc-tab-wrapper">';    
        $html .= '<ul class="sc-tabs">';
            $datatab = 0;
            foreach($parent_product_categories as $parentprodcat) {
            $datatab++;
            $top_term_id = $parentprodcat->term_id;
            $top_term_name = $parentprodcat->name;
            $top_term_tax = $parentprodcat->taxonomy;

            $html .= '<li id="'.$top_term_id.'" class=" sc-tab-link '. ( ($datatab == 1) ?  "current"  :  '') .' " data-tab="tab-'.$datatab.'">'.$top_term_name.'</li>';


            $second_level_terms[] = get_terms( array(
                    'taxonomy' => $top_term_tax, // you could also use $taxonomy as defined in the first lines
                    'child_of' => $top_term_id,
                    'parent' => $top_term_id, // disable this line to see more child elements (child-child-child-terms)
                    'hide_empty' => false,
            ) );    

            } // end of top level foreach   
            $html .= '</ul>';

            /*echo '<pre>';
                print_r($second_level_terms);
            echo '</pre>';*/

            if($second_level_terms) {   
                $contenttab = 0;    
                $html .= '<div class="sc-tab-content-wrapper">';    
                    foreach($second_level_terms as $row => $innerArray){
                        $contenttab++;
                        $html .= '<div id="tab-'.$contenttab.'" class="sc-tab-content '. ( ($contenttab == 1) ?  "current"  :  '') .' ">';
                            foreach($innerArray as $innerRow => $value){
                                    $second_term_name = $value->name;
                                    $second_level_term_id = $value->term_id;
                                    $html .= '<p>'.$value->name.'</p>';

                                    $third_level_terms[] = get_terms( array(
                                        'taxonomy' => $top_term_tax, // you could also use $taxonomy as defined in the first lines
                                        'parent' => $second_level_term_id,
                                        'child_of' =>  $second_level_term_id,
                                        'hide_empty' => false,
                                    ) );

                                    /** THIS BLOCK CODE IS WHAT I'M HAVING ISSUE WITH **/

                                    if($third_level_terms) {
                                        foreach ($third_level_terms as $row => $innerArray){
                                            foreach($innerArray as $innerRow => $value){
                                                $html .= '<p style="padding-left: 15px; font-size: 12px;">'. $value->name.'</p>';
                                            }
                                        }
                                    } /** THIS BLOCK CODE IS WHAT I'M HAVING ISSUE WITH **/
                         }
                        $html .= '</div>';
                    }
                $html .= '</div>';
            } // end of if second leveL term        

            echo '<pre>';
                print_r($third_level_terms);
            echo '</pre>';

    return $html;

Current Result: 当前结果:

Parent Category > Child Category > Inner Child Category(ALL INNER CHILD CATEGORIES SHOWS PER CHILD CATEGORY)

Expected Result: 预期结果:

Parent Category > Child Category > Inner Child Category (Should display per Child Category) Parent Category > Child Category > Inner Child Category (应按每个子类别显示)

I hope this was clear to understand! 我希望这很清楚! Please do tell me where I did wrong. 请告诉我我做错了什么。 I'm so confused on how to access the third level of the array. 我对如何访问数组的第三级感到很困惑。

I found the issue and solved my problem! 我找到了问题并解决了我的问题!

Had to comment out the code for the loop to work as intended! 不得不注释掉代码以使循环按预期工作!

$second_level_terms[] = get_terms( array(
                    'taxonomy' => $top_term_tax, 
                    'child_of' => $top_term_id,
                    //'parent' => $top_term_id, // This line of code was the problem! ugh!
                    'hide_empty' => false,
            ) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM