Foreach - 递归? [英] Foreach - recursion?

查看:142
本文介绍了Foreach - 递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码,它是从一个基于woocommerce的网站以层次结构的方式获取所有的类别和子类别,它可以工作,但是每当我想添加一个新的深度级别时,

  category 
- > sub-cat
- > sub-sub-cat
- > sub-sub- sub-cat
- > etc

我必须在最后一个循环(我甚至不知道如果你能理解我诚实的原因,我甚至不知道如何解释这个 - 对不起,请耐心等待,这是我在phps的第二个月的领域)



所以,我试图摆脱所有这些foreach循环(使其更加动态化),我听说有一种方法可以通过使用称为递归的东西我可以用来做到这一点的东西。我真的不明白这是如何工作,我已经阅读了整整一天,所以如果有人能帮助我理解如何做到这一点,我会很高兴?



这是我的代码,它的一部分来自StackOverflow。

 < ul> 
<?php
$ taxonomy ='product_cat';
$ orderby ='name';
$ hierarchical = 1;

$ args = array(
'taxonomy'=> $ taxonomy,
'orderby'=> $ orderby,
'hierarchical'=> $等级
);

$ cat = get_categories($ args);
foreach($ cat as $ c){
if($ c-> category_parent == 0){
$ catID = $ c-> term_id;
echo'< li>< a href ='。get_term_link($ c-> slug,'product_cat')。'>'。 $ c->名称。< / a>';

$ args = array(
'taxonomy'=> $ taxonomy,
'parent'=> $ catID,
'orderby'=> $ orderby,
'hierarchical'=> $ hierarchical
);

$ cat = get_categories($ args);

if($ cat){
echo'< ul>';
foreach($ cat as $ c){

$ catID = $ c-> term_id;
$ b echo'< li>< a href ='。get_term_link($ c-> slug,'product_cat')。'>'。 $ C->名称,apply_filters(woocommerce_subcategory_count_html,( $ C->计数‘)’,$类别)。< / A>;

$ args = array(
'taxonomy'=> $ taxonomy,
'parent'=> $ catID,
'orderby'=> $ orderby,
'hierarchical'=> $ hierarchical
);

$ cat = get_categories($ args);

if($ cat){
echo'< ul>';
foreach($ cat as $ c){

$ catID = $ c-> term_id;
$ b echo'< li>< a href ='。get_term_link($ c-> slug,'product_cat')。'>'。 $ C->名称,apply_filters(woocommerce_subcategory_count_html,( $ C->计数‘)’,$类别)。< / A>;

$ args = array(
'taxonomy'=> $ taxonomy,
'parent'=> $ catID,
'orderby'=> $ orderby,
'hierarchical'=> $ hierarchical
);

$ cat = get_categories($ args);

if($ cat){
echo'< ul>';
foreach($ cat as $ c){

$ catID = $ c-> term_id;
$ b echo'< li>< a href ='。get_term_link($ c-> slug,'product_cat')。'>'。 $ C->名称,apply_filters(woocommerce_subcategory_count_html,( $ C->计数‘)’,$类别)。< / A>< /立GT;;

}
echo'< / ul>';
}
echo'< / li>';
}
echo'< / ul>';
}
echo'< / li>';
}
echo'< / ul>';
}
echo'< / li>';
}
}
?>



我知道这不是太聪明,所以请不要太苛刻。

解决方案

打印功能可能是这样的:

 < PHP 
功能display_child_categories_of($ CATEGORY_ID){
$ subcategories = get_categories(array('child_of'=> category_id));
foreach($ subcategories as $ subcategory){
echo'< li>;
echo $ subcategory-> name;
echo'< ul class =children>';
display_child_categories_of($ subcategory);
echo'< / ul>';
echo'< / li>';
}
}
?>

< ul>
<?php
display_child_categories_of(0); //获取所有顶级类别
?>
< / ul>

这是一个准系统版本;你需要添加你使用的附加参数给 get_categories()参数,然后根据需要调整HTML。



然而,正如评论中提到的@jaswrks,如果它适合您的情况,您应该使用内置的 walk_category_tree() WordPress功能。 / p>

I have this code that is getting all categories and subcategories from a woocommerce based website in a hierarchical order, and it works but each and every time when I want to add a new level of depth

category
    ->sub-cat
        ->sub-sub-cat
            ->sub-sub-sub-cat
                ->etc

I have to add one more foreach loop inside the last loop (I'm not even sure if you can understand me to cause honestly I don't even know how to explain this - sorry, please be patient, it is my second month in PHPs 'fields')

So, I'm trying to get rid of all those foreach loops (to make it more dynamic) and I heard that there is a way to do that by using something called recursion, the kind of stuff that I can use to make this happen. I don't really understand how that works and I've read like the whole day about it so if there is somebody who can help me understand how to get this done I'll be happy?

Here is my code, part of it is taken from StackOverflow.

<ul>  
<?php
$taxonomy       = 'product_cat';
$orderby        = 'name';     
$hierarchical   = 1; 

$args = array(
    'taxonomy'     => $taxonomy,
    'orderby'      => $orderby,
    'hierarchical' => $hierarchical
);

$cat = get_categories( $args );
foreach ($cat as $c) {
    if($c->category_parent == 0) {
        $catID = $c->term_id;       
        echo '<li><a href="'. get_term_link($c->slug, 'product_cat') .'">'. $c->name .'</a>';

        $args = array(
            'taxonomy'     => $taxonomy,
            'parent'       => $catID,
            'orderby'      => $orderby,
            'hierarchical' => $hierarchical
        );

        $cat = get_categories( $args );

        if($cat) {
            echo '<ul>';
            foreach($cat as $c) {

                $catID = $c->term_id;

                echo  '<li><a href="'. get_term_link($c->slug, 'product_cat') .'">'. $c->name , apply_filters( 'woocommerce_subcategory_count_html', ' (' . $c->count . ')', $category ) .'</a>';

                $args = array(
                    'taxonomy'     => $taxonomy,
                    'parent'       => $catID,
                    'orderby'      => $orderby,
                    'hierarchical' => $hierarchical
                );

                $cat = get_categories( $args );

                if($cat){
                    echo '<ul>';
                    foreach ($cat as $c) {

                        $catID = $c->term_id;

                        echo  '<li><a href="'. get_term_link($c->slug, 'product_cat') .'">'. $c->name , apply_filters( 'woocommerce_subcategory_count_html', ' (' . $c->count . ')', $category ) .'</a>';

                        $args = array(
                            'taxonomy'     => $taxonomy,
                            'parent'       => $catID,
                            'orderby'      => $orderby,
                            'hierarchical' => $hierarchical
                        );  

                        $cat = get_categories( $args );

                        if($cat){
                            echo '<ul>';
                            foreach ($cat as $c) {

                                $catID = $c->term_id;

                                echo  '<li><a href="'. get_term_link($c->slug, 'product_cat') .'">'. $c->name , apply_filters( 'woocommerce_subcategory_count_html', ' (' . $c->count . ')', $category ) .'</a></li>';

                            }
                            echo '</ul>';
                        }
                        echo '</li>';
                    }
                    echo '</ul>';
                }
                echo '</li>';
            }
            echo '</ul>';
        }
    echo '</li>';
    }       
}
?>

It works perfectly, though I'm aware that it is not too smart, so please don't be too harsh on me.

解决方案

A WordPress-specific printing function might look like this:

<?php
function display_child_categories_of($category_id) {
    $subcategories = get_categories(array('child_of' => category_id));
    foreach ($subcategories as $subcategory) {
        echo '<li>';
        echo $subcategory->name;
        echo '<ul class="children">';
        display_child_categories_of($subcategory);
        echo '</ul>';
        echo '</li>';
    }
}
?>

<ul>
    <?php
    display_child_categories_of(0); // gets all top-level categories
    ?>
</ul>

This is a barebones version; you'll want to add in the additional parameters you're using to the get_categories() arguments and then tweak the HTML as needed.

However, as @jaswrks mentioned in the comments, you should use the built-in walk_category_tree() WordPress function instead if it works for your situation.

这篇关于Foreach - 递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆