日期:2014-05-17  浏览次数:20435 次

类方法返回值,奇怪的现象
本帖最后由 xuzuning 于 2013-03-08 14:59:52 编辑
各位大侠,请看如下代码:
我要实现的功能是,利用一个多维数组输出一个树状结构,下面的参数是多维数组。

//递归树状输出格式一

	public function accountTreeType1($arrData){

$this->strLable = $this->strLable.'<ul>';

foreach($arrData as $val){

if(is_array($val['child'])){
$this->strLable = $this->strLable.'<li>'.$val['acc_code'].$val['acc_name'];
$this->accountTreeType1($val['child']);
}else{

$this->strLable = $this->strLable.'<li>'.$val['acc_code'].$val['acc_name'].'</li>';
                                 if($val[id]=='最后一个ID'){
                                      return $this->strLable; //在这里没有返回值,不过用echo $this->strLable;是可以打印出来,但是返回值为空。
                                 }

}

}

$this->strLable = $this->strLable.'</ul>';

}

------解决方案--------------------
方法的最后加上
return $this->strLable;
------解决方案--------------------
 public function accountTreeType1($arrData){
        $strLable .= '<ul>';
        foreach($arrData as $val){
            if(is_array($val['child'])){
                $strLable .= '<li>'.$val['acc_code'].$val['acc_name'].'</li>';
                $strLable .= $this->accountTreeType1($val['child']);
            }else{
                $strLable .= '<li>'.$val['acc_code'].$val['acc_name'].'</li>';
            }
        }
        return $strLable.'</ul>';
 }