php递归,函数不返回任何值 [英] php recursion, function doesn't return any value

查看:116
本文介绍了php递归,函数不返回任何值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的函数返回整个 url,从具有给定 id 的节点开始,然后搜索父母,最后一个有 parent_id =1.我的功能几乎可以工作,在 echo"$wholeUrl ";我有我的网址,但函数没有重新调整它,我不知道 wtf,请帮忙.

I want my function to return whole url, starting from node with given id, and serching for parents, last one has parent_id =1. My function almost works, in echo"$wholeUrl "; i have my url, but function doesn't retun it and I don't know wtf, please help.

这是我的代码:

函数 getUrl($xml,$id){

function getUrl($xml,$id){

$wholeUrl="";
$wholeUrl= getMyUrl($xml,$id,$wholeUrl);
return $wholeUrl;

}

函数 getMyUrl($xml,$idWew, $wholeUrl){

function getMyUrl($xml,$idWew, $wholeUrl){

foreach($xml as $node) {
    $par = $node->parent_id;
    $ide = $node->id;
    $url = $node->url;
    $name = $node->name;
    settype($par,'integer');
    settype($ide,'integer');

    if($ide==$idWew){

        $wholeUrl=$url."/".$wholeUrl;

        if($par==1){
            echo"$wholeUrl ";
            return $wholeUrl;
            break;
        }else{

            getMyUrl($xml,$par,$wholeUrl);
        }
    }

}
}


print_r(getUrl($xmlcat,1877));

$xmlcat 是具有以下结构的平面数组:

$xmlcat is flat array with this structure:

SimpleXMLElement Object ( [id] => 1876 [parent_id] => 1 [name] => blablabla, bla, bla [url] => bla-bla-bla-bla ) 

推荐答案

你需要返回getMyUrl:

You need to return getMyUrl:

    /* .... */
    }else{

        return getMyUrl($xml,$par,$wholeUrl);
    }
    /* .... */

这篇关于php递归,函数不返回任何值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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