Home > Backend Development > PHP Tutorial > 类方法返回值,奇怪的现象解决思路

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

WBOY
Release: 2016-06-13 12:52:18
Original
887 people have browsed it

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

本帖最后由 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 />
	}
Copy after login

------解决方案--------------------
方法的最后加上
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.'
';
 }
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template