递归函数获取父类描述 [英] Recursive function to get the parent caegory description

查看:35
本文介绍了递归函数获取父类描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个递归函数,我需要的是获取该产品的父类别描述和 term_id.

I have created a recursive function, and what i need is to get the parent category description and term_id of that product.

function desc_pro($parent) {
    $term = get_term_by('parent', $parent, 'product_cat');
    $description = $term->description;

    while($description == null) {
        $desc = desc_pro($term->parent);
        return $desc;
    }
    return $description;
}

现在当我运行这段代码时,我得到了正确的描述.但如果我删除任何回报,那么它将不起作用.它显示空白.(这样可以吗?我觉得代码有问题?)

Now when i run this code, i get the correct description. But if i remove any of the return then it will not work. It shows blank. (This is okay? I think the code is wrong?)

第二:我需要 term_id 并且当我创建数组时,它也会发送所有子类别 ID.这是错误的.我只需要具有描述的 id.

Second: I need term_id and when i create the array, it send all the sub category ids as well. Which is wrong. I need just that id which has the description.

我认为代码是错误的?还是有其他问题?

I think the code is wrong? Or there is any other problem?

这是我的数组:(我发送的是来自我的 php 页面的父类别.所以我调用函数 get_desc(48);)

This is the array with me: (what i send is the parent category from my php page. So i call function get_desc(48);)

它给了我第一个对象,现在我必须测试描述是否可用?如果是,则停止并返回描述,它是 term_id.如果它不可用,则获取父 ID 并再次检查.如此继续,直到找到描述为止.

It give me first object, now i have to test is description available or not? If yes then stop and return description and it's term_id. If it is not available then grab parent id and check again. So this continues, until description is found.

stdClass Object
(
    [term_id] => 48
    [name] => Cereal
    [slug] => cereal
    [term_group] => 0
    [term_taxonomy_id] => 49
    [taxonomy] => product_cat
    [description] => 
    [parent] => 46
    [count] => 0
)

stdClass Object
(
    [term_id] => 46
    [name] => Grocery Store A
    [slug] => grocery-store-a
    [term_group] => 0
    [term_taxonomy_id] => 47
    [taxonomy] => product_cat
    [description] => FDIC, 17th Street Northwest, Washington, DC
    [parent] => 45
    [count] => 0
)

推荐答案

因为你的 while 循环不会改变 $description 而是 $desc,它总是 $descriptioncode>null 如果它不是基本情况并且您创建了一个无限循环.

Since your while loop doesn't change $description but $desc, it will always be null if it's not the base case and you have created an infinite loop.

试试这个:

function desc_pro($parent) {
    $term = get_term_by('parent', $parent, 'product_cat');
    $description = $term->description;

    if( $description == null)
        return desc_pro($term->parent); // recurse if not set
        //$description = desc_pro($term->parent); // an alternative to the above

    return $description; // base case (when set)
}

返回和赋值的区别是额外的返回.PHP 不做尾调用优化,所以它并没有什么不同,只是没有注释掉的那个对于函数式程序员来说看起来更好.

The differences between the return and the assignment is the extra return. PHP doesn't do tail call optimization so it's not really that different, only that the one not commented out looks better for a functional programmer.

对于迭代方法,while 循环会很好:

For an iterative approach a while loop would be good:

function desc_pro($parent) {
    $description = null;

    while( $description == null) {
        $term = get_term_by('parent', $parent, 'product_cat');
        $description = $term->description;
    }

    return $description; // base case (when set)
}

这篇关于递归函数获取父类描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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