Heroes, please see the following code:
The function I want to implement is to use a multidimensional array to output a tree structure, and the following parameter is a multidimensional array.
Recursive tree-like output format one
Public Function AccountTreeType1 ($arrData) {$this->strlable = $this->strlable. '
'; foreach ($arrData as $val) {if (Is_array ($val [' child ')]) {$this->strlable = $this->strlable. '
- '. $val [' Acc_code ']. $val [' Acc_name ']; $this->accounttreetype1 ($val [' child ']);} else{$this->strlable = $this->strlable. '
- '. $val [' Acc_code ']. $val [' Acc_name ']. '
'; if ($val [id]== ' last ID ') {return $this->strlable;//There is no return value here, but with Echo $this->strlable; can be printed, but the return value is empty. }}} $this->strlable = $this->strlable. '
;}
Reply to discussion (solution)
Line 9th $this->accounttreetype1 did not undertake the return of the 14th row
Line 9th $this->accounttreetype1 did not undertake the return of the 14th row
So why is echo going to print?
How do you call this method?
After function return, it means that the method has run out and the subsequent code will not execute.
$str = D (' account ')->accounttreetype1 ($Data);
The last addition of the method
return $this->strlable;
1. is strlable defined in class?
2, return you do not receive the return value what are you going to do?
3, echo of course can output, you do not understand what is recursive function?
After the addition, there is a return value. But it's not quite understandable why this is written.
Public Function AccountTreeType1 ($arrData) {
$strLable. = '
';
foreach ($arrData as $val) {
if (Is_array ($val [' child ')]) {
$this->accounttreetype1 ($val [' child ']);
}else{
$strLable. = '
- '. $val [' Acc_code ']. $val [' Acc_name ']. '
';
}
}
return $strLable. '
';
}
You don't even return how to get the value
You don't even return how to get the value
I just got the code return. Just can't figure out when to return. and add return on the last side of the method, it's recursive, and every call will return, right?
$val [What is Id],id? It should be $id or "id" ... So there's no return to $this->strlabel.
Return is to jump out of the current method you just actually called the current function several times you did not understand the recursion
You better understand.
Return back continue and recursion
Public Function AccountTreeType1 ($arrData) {
$strLable. = '
';
foreach ($arrData as $val) {
if (Is_array ($val [' child ')]) {
$this->accounttre ...
This method cannot return all the values
$arrData print it out and see
Array
(
[1] = = Array
(
[id] = 1
[Acc_code] = 1001
[Acc_name] = cash in stock
[Acc_level] = 1
[Acc_detail] = 0
[acc_parent_id] = 0
[ACC_ROOT_ID] = 1
[Acc_state] = 1
[Child] = = Array
(
[+] = Array
(
[id] = 38
[Acc_code] = 100101
[Acc_name] = Shenzhen banknotes
[Acc_level] = 2
[Acc_detail] = 0
[ACC_PARENT_ID] = 1
[ACC_ROOT_ID] = 1
[Acc_state] = 1
[Child] = = Array
(
[+] = Array
(
[id] = 39
[Acc_code] = 10010101
[Acc_name] = Jian Juntao
[Acc_level] = 3
[Acc_detail] = 0
[acc_parent_id] = 38
[ACC_ROOT_ID] = 1
[Acc_state] = 1
[Child] = = Array
(
[804] = = Array
(
[id] = 804
[Acc_code] = 1001010101
[Acc_name] = Fdsaf
[Acc_level] = 4
[Acc_detail] = 1
[acc_parent_id] = 39
[ACC_ROOT_ID] = 1
[Acc_state] = 1
)
)
)
[+] = Array
(
[id] = 40
[Acc_code] = 10010102
[Acc_name] = Lin Kuanmao
[Acc_level] = 3
[Acc_detail] = 1
[acc_parent_id] = 38
[ACC_ROOT_ID] = 1
[Acc_state] = 1
)
[+] = Array
(
[id] = 41
[Acc_code] = 10010103
[Acc_name] = Wu Xue as
[Acc_level] = 3
[Acc_detail] = 1
[acc_parent_id] = 38
[ACC_ROOT_ID] = 1
[Acc_state] = 1
)
[All] = Array
(
[id] = 42
[Acc_code] = 10010104
[Acc_name] = Guan Yong
[Acc_level] = 3
[Acc_detail] = 1
[acc_parent_id] = 38
[ACC_ROOT_ID] = 1
[Acc_state] = 1
)
[+] = Array
(
[id] = 43
[Acc_code] = 10010105
[Acc_name] = Hu Zhenming
[Acc_level] = 3
[Acc_detail] = 1
[acc_parent_id] = 38
[ACC_ROOT_ID] = 1
[Acc_state] = 1
)
[+] = Array
(
[id] = 44
[Acc_code] = 10010106
[Acc_name] = Huang Jinxhong
[Acc_level] = 3
[Acc_detail] = 1
[acc_parent_id] = 38
[ACC_ROOT_ID] = 1
[Acc_state] = 1
)
[+] = Array
(
[id] = 45
[Acc_code] = 10010107
[Acc_name] = Chen Paihui
[Acc_level] = 3
[Acc_detail] = 1
[acc_parent_id] = 38
[ACC_ROOT_ID] = 1
[Acc_state] = 1
)
[+] = = Array
(
[id] = 46
[Acc_code] = 10010108
[Acc_name] = Chen Zhenjiang
[Acc_level] = 3
[Acc_detail] = 1
[acc_parent_id] = 38
[ACC_ROOT_ID] = 1
[Acc_state] = 1
)
[789] = = Array
(
[id] = 789
[Acc_code] = 435435
[Acc_name] = Fdsaf
[Acc_level] = 3
[Acc_detail] = 1
[acc_parent_id] = 38
[ACC_ROOT_ID] = 1
[Acc_state] = 1
)
)
)
[+] = Array
(
[id] = 47
[Acc_code] = 100102
[Acc_name] = Guangzhou banknotes
[Acc_level] = 2
[Acc_detail] = 0
[ACC_PARENT_ID] = 1
[ACC_ROOT_ID] = 1
[Acc_state] = 1
[Child] = = Array
(
[+] = Array
(
[id] = 48
[Acc_code] = 10010201
[Acc_name] = Wukunlen
[Acc_level] = 3
[Acc_detail] = 1
[acc_parent_id] = 47
[ACC_ROOT_ID] = 1
[Acc_state] = 1
)
)
)
)
)
)
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. '
';
}
Public Function AccountTreeType1 ($arrData) {
$strLable. = '
';
foreach ($arrData as $val) {
if (Is_array ($val [' child ')]) {
$strLable. = '
This can be achieved. Thank you, I kind of understand, where I was wrong.
-