Writing each key once in twig

52 Views Asked by At

I have a json data and I want to write data keys once using twig. But when I try to store keys in uniqueArray to check the key is exists in the array, it is not working.

Here is my twig code, where is the problem ?


{% macro fetch(uArray, data) %}
    <ul>
        {{ _self.keys(uArray, data) }}
    </ul>
{% endmacro %}


{% macro keys(uniqueArray, data) %}
    {% if data is iterable %}
        {% for key, val in data %}
            {% if key not in uniqueArray %}
                {% set uniqueArray = uniqueArray|merge([key]) %}
                <li>
                    {{ key }}
                </li>
                {% if val is iterable %}
                    {{ _self.fetch(uniqueArray, val) }}
                {% endif %}
            {% endif %}
        {% endfor %}
    {% endif %}
{% endmacro %}

<div class="table-map">
    <ul>
        {% set unique = [] %}
        {{ _self.keys(unique, data) }}
    </ul>
</div>

0

There are 0 best solutions below