Answer the question
In order to leave comments, you need to log in
What is the difference between these php examples?
There is a List tree
:
name
description
List
I did all the examples myself, in general, this is not even an example, but the same function on which the experiment was conducted
private function recursive($list)
{
foreach($list as $element){
if(is_array($element)){
$this->recursive($element);
}else{
dump($element);
}
}
}
private function recursive($list)
{
if(isset($list['list'])){
foreach($list['list'] as $element){
$this->recursive($element);
}
}else{
dump($list['name']);
dump($list['description']);
}
}
private function recursive($list)
{
foreach($list as $element){
dump($element);
if(isset($element['list'])){
$this->recursive($element['list']);
}
}
}
private function recursive($list)
{
if(isset($list['list'])){
$this->recursive($list['list']);
}else{
dump($list['name']);
dump($list['description']);
}
}
Answer the question
In order to leave comments, you need to log in
Well, in the last version, you seem to have no output. So that there will be at promotion of a recursion - to me it is not clear. The first two are quite different. In the first case, a check occurs - if the element is List, then dive deeper and it is obvious, in the second you check some flag ($list['closureTag']), which is not described in the question, so I can’t say which elements will pass the check can.
UPD: The code has been heavily rewritten, so my answer is mostly not up to date, but I'll make changes anyway. Note that the first code does not use any indexes or string constants. Do you know why? because any typo and immediately - "Happy debugging". Maintaining this is also difficult, because renaming fields becomes fun and provocative. Fuck knows where they were used in the old form. Further - in what the main bug. Your conclusion is ONLY if the node has no heirs. Those. you only remove the ends. It is right?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question