D
D
dllweb2015-03-04 11:11:39
symfony
dllweb, 2015-03-04 11:11:39

Arrays trees in php why does the tree disappear?

I just can’t understand why the tree is collapsing, in the symphony project I do this:
There is a service that is responsible for sorting by parent keys in an array

public function persistviewtree($arr){
        return $this->viewtree($arr, 0);
    }


    public function viewtree($arr, $root){

        if(is_array($arr) and isset($arr[$root])){


            $str = "<ul>";

                    foreach ($arr[$root] as $r) {
                    $str .= "<li data-node=".$r['id']." style=\"position:relative;\" class=\"list-group-item\">
                    <a href=\"/wikiedit/".$r['id']."\">".$r['name']."</a>
                    <i class=\"glyphicon glyphicon-remove pull-right\" style=\"cursor:pointer;\" onclick=\"R.wiki.delete(".$r['id'].");\</i>";              
                    $str .= $this->viewtree($arr, $r['id']);
                    $str .= "</li>";
                    }

            $str .= "</ul>";

        } else {

            return false;

        }

        return $str;

    }

There is an array of the view,
CN11YiEWxgg.jpg
the call to the persistviewtree function occurs in the controller
from there, the usual variable is already given to the template.
As a note, I will say that if the key in the first cell of the presented array is 0
, then everything works correctly, but if, as it is now in the picture, the tree is displayed either partially or completely disappears.
If the same experiment is repeated in a procedural environment, then everything works without problems at all.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-03-04
Protko @Fesor

the persistviewtree function call happens in the controller

Fu Fu Fu. It's better to take it to at least a helper for twig.
In general, your algorithm should work correctly and nothing should depend on the order of the nodes. Issue a non-working example on which ideone.com - because according to the code, the problem is clearly not in it.

A
Alexey Pavlov, 2015-03-04
@lexxpavlov

Outputting such arrays is a task for Twig. This is done in the simplest way:

{% extends '::layout.html.twig'%}

{% block content %}
<ul>
{% for items in tree %}
{% for item in items %}
    <li data-node="{{ item.id }}" style="position:relative;" class="list-group-item">
         <a href="/wikiedit/{{ item.id }}">{{ item.name }}</a>
         <i class="glyphicon glyphicon-remove pull-right" style="cursor:pointer;" onclick="R.wiki.delete({{ item.id }});"></i>
    </li>
{% endfor %}
{% endfor %}
</ul>
{% endblock %}

This is if there are no nested elements. If there are nested ones, then you need to make a separate template (without extends), and then call it via include.
And you have another error in the output - the method $str .= $this->viewtree($arr, $r['id']);is called first for arrays of the first nesting, and they do not have an id key. Here you need to make two different methods - one will process the first level of nesting, and the second method - the elements themselves with data.
And if you do not have nested elements with data, then you should not use recursion. You can clearly see that elements of different nesting levels have different data formats, which means that it is incorrect to process them with one method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question