Heim  >  Artikel  >  Backend-Entwicklung  >  类方法返回值,奇怪的现象解决思路

类方法返回值,奇怪的现象解决思路

WBOY
WBOYOriginal
2016-06-13 12:52:18823Durchsuche

类方法返回值,奇怪的现象

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

//递归树状输出格式一

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

------解决方案--------------------
方法的最后加上
return $this->strLable;
------解决方案--------------------
 public function accountTreeType1($arrData){
        $strLable .= '
    ';
            foreach($arrData as $val){
                if(is_array($val['child'])){
                    $strLable .= '
  • '.$val['acc_code'].$val['acc_name'].'
  • ';
                    $strLable .= $this->accountTreeType1($val['child']);
                }else{
                    $strLable .= '
  • '.$val['acc_code'].$val['acc_name'].'
  • ';
                }
            }
            return $strLable.'
';
 }
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn