A
A
Abc Edc2015-10-02 11:28:55
PHP
Abc Edc, 2015-10-02 11:28:55

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

Example 1 (working)
private function recursive($list)
    {
        if(isset($list['list'])){
            foreach($list['list'] as $element){
                $this->recursive($element);
            }
        }else{
            dump($list['name']);
            dump($list['description']);
        }
    }

Example 2 is not working because for some reason it reveals 5 elements of the tree, although at the time there were only 11 of them.
The question is what's wrong with him?
private function recursive($list)
{
   foreach($list as $element){
          dump($element);
          if(isset($element['list'])){
               $this->recursive($element['list']);
          }
   }
}

Example 3
It's funny, but this example only outputs once, although dubbing, as for me, of the first option. Why is that?
private function recursive($list)
    {
        if(isset($list['list'])){
            $this->recursive($list['list']);
        }else{
            dump($list['name']);
            dump($list['description']);
        }
    }

Example 4 (18+) puts the server on the blades and knocks out everything in the world

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kovalsky, 2015-10-02
@dmitryKovalskiy

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 question

Ask a Question

731 491 924 answers to any question