Leon Hogarth
Leon Hogarth

Reputation: 35

in twig how to print out keys and values of an array

I just can't seem to find the right twig code to simply print out values from an array.

I have an array which contains the applied filters to a page.

I can dump the array by doing {{ dump(filters) }}

This dumps the array like this:

array(3) { [0]=> array(1) { ["colour"]=> string(5) "White" } [1]=> array(1) { ["colour"]=> string(3) "Red" } [2]=> array(1) { ["country"]=> string(6) "France" } }

Which shows I have 3 filters applied. colour=red, colour=white and country=france.

All I want to do is for every element in filters print these to the page like so.

colour:Red colour:White country:France.

I can then turn them into links that will remove the filter.

The code I have so far is

{% if filters is iterable %}
        {% for elem in filters %}
            {{ ?????????? }}:{{ ?????????? }}
        {% endfor %}
    {% endif %}

Most things I try errors, or complains I am converting arrays to strings

Thanks

Upvotes: 0

Views: 2478

Answers (2)

Carlos Granados
Carlos Granados

Reputation: 11351

Your filters variable is an array of arrays, so you need to do something like:

{% for filter in filters %}
    {% for key, value in filter %}
        {{ key }} : {{ value }}
    {% endfor %}
{% endfor %}

Upvotes: 2

jmattheis
jmattheis

Reputation: 11115

you can do this:

{% for key,value in filters %}
   {{ key }} : {{ value }}
{% endfor %}

Upvotes: 3

Related Questions