ホームページ  >  記事  >  バックエンド開発  >  クラスメソッドの戻り値、不思議な現象

クラスメソッドの戻り値、不思議な現象

WBOY
WBOYオリジナル
2016-06-23 14:02:111148ブラウズ

親愛なる皆さん、次のコードを見てください:
私が実装したい関数は、多次元配列を使用してツリー構造を出力することです。以下のパラメーターは多次元配列です。

//再帰ツリー出力形式1

	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>';	}


ディスカッションへの返信(解決策)

9行目の$this->accountTreeType1は14行目のreturnを継承しません

9行目の$ this- >accountTreeType1 は 14 行目のリターンを受け入れません
では、なぜエコー時に出力できるのでしょうか?
このメソッドはどのように呼び出すのでしょうか?

関数が戻った後は、メソッドの実行が終了したことを意味し、後続のコードは実行されません。

$str = D('Account')->accountTreeType1($Data);

メソッドの最後に

return $this->strLable; を追加します


1. strLable はクラスで定義されていますか?

2. 戻り値を受け取らなかったらどうしますか?

3. もちろん、echo は出力できます。再帰関数が何なのか理解していませんか?

追加後は戻り値があります。しかし、なぜこのように書かれているのかはよくわかりません。 public counttreetype1($ arrdata){&gt; ;/ul>ただ、いつ戻ればいいのか分かりません。そして、メソッドの最後に return を追加すると、それは再帰的となり、呼び出されるたびに戻りますよね?

$val[id]、id とは何ですか? $id または "id" である必要があります...したがって、$this->strLabel はまったく返されません

return は、実際に現在の関数を数回呼び出したところです。再帰を理解していない
理解したほうがいいです
return back continue と recursion

public function accountTreeType1($arrData){
_array($val['child'] ) {
$ this- & gt;
このメソッドはすべての値を返すことはできません
$ ardata を出力します

配列
(
[1] => 配列
(
[id] => 1
[acc_code] => 1001
[acc_name] => 库存现金
[acc_level] => 1
[acc_detail ] => 0
[acc_parent_id] => 0
1
1
[子] => 配列
(
[38] => 配列
(
[id] =&gt; [acc_root_id] => 1
[acc_state] => 1
[子] => (
[39] => 配列
(
[id] => 39
[acc_code] => 10010101
                                            [acc_name] => 詹军涛
[acc_level] => 3
[acc_detail] => 0
[acc_parent_id] => 38
[acc_root_id] => 1
[acc_state] => 1
[子] => 配列

[804] =&gt; array

[id] =&gt;804
[acc_code] =&gt; 1001010101
[acc_name] =&gt;fdsaf
[acc_level] =&gt; 4
                                                            [acc_detail] => 1
[acc_parent_id] => 39
[acc_root_id] => 1
[acc_state] => 1
)
)
)
[40] => array

[id] =&gt;40
[acc_code] =&gt; 10010102 38
                                            [acc_root_id] => 1
[acc_state] => 1
)
[41] => array

[id] =&gt; 41
[acc_code] =&gt; 10010103
[acc_name] =&gt;吴雪吴雪如如如如[acc_level] =&gt; 3 38
[acc_root_id] => 1
[acc _state] => 1
)
[42] => 配列
(
[id] => 42
                                            [acc_code] => 10010104
[acc_name] => 关蓉
[acc_level] => 3
[acc_detail] => 1
[acc_parent_id] => 38
[acc_root_id] => 1
[acc_state] => 1
)
[43] => array

[id] =&gt;43
[acc_code] =&gt; 10010105 ; 38
                                            [acc_root_id] => 1
[acc_state] => 1
)
[44] => array

[id] =&gt; 44
[acc_code] =&gt; 10010106
[acc_name] =&gt; 38
[acc_root_id] => 1
[acc _state] => 1
)
[45] => 配列
(
[id] => 45
                                            [acc_code] => 10010107
[acc_name] => 陈培怀
[acc_level] => 3
[acc_detail] => 1
[acc_parent_id] => 38
[acc_root_id] => 1
[acc_state] => 1
)
[46] => array

[id] =&gt; 46
[acc_code] =&gt;10010108
[acc_name] =&gt;陈镇江陈镇江
陈镇江[acc_level] =&gt; 3
[acc_detail] =&gt; 1
[acc_parent_id] =&gt ; 38
                                            [acc_root_id] => 1
[acc_state] => 1
)
[789] => array

[id] =&gt; 789
[acc_code] =&gt; 435435 38
[acc_root_id] => 1
[acc _state] => 1
)
)
)

[47] => 配列
(
[id] => 47
                            [acc_code] => 100102
[acc_name] => 广州现钞
[acc_level] => 2
[acc_detail] => 0
[acc_parent_id] => 1
[acc_root_id] => 1
[acc_state] => 1
[子供] => array

[48] =&gt; array

[id] =&gt;48
[acc_code] =&gt; 10010201
[acc_name] =&gt;吴昆伦吴昆伦吴昆伦
> 1
[acc_parent_id] => 47
[acc_root_id] => 1
[acc_state] => 1
                                        )
)

)

)
)


public function accountTreeType1($arrData){

$strLable .= 'ff6d136ddc5fdfeffaf53ff6ee95f185'; foreach($ arrdata as $ val){

if(is_array($ val ['child'])){
$ strlable。= '&lt; li&gt;'。$ val ['acc_code']。$ val ['acc_name '].'bed06894275b65c1ab86501b08a632eb';
$strLable .= $this->accountTreeType1($val['child']);
}else{
$strLable .= '25edfb22a4f469ecb59f1190150159c6'.$val['acc_code'].$val['acc_name'].'bed06894275b65c1ab86501b08a632eb';
}
}
return $strLable.'929d1f5ca49e04fdcb27f9465b944689';
}

public function accountTreeType1($arrData){
$strLable .= 'ff6d136ddc5fdfeffaf53ff6ee95f185';

foreach($arrData as $val){

if(is_array($val['child'])){
$strLable .= ' これは実現可能です。谢你们,我有点明白,我错在哪了。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。