How to check a multidimensional Twig array for values?

arrays, twig

Solution

Set a flag and use a loop. Afterwards you can use the flag in if conditions.

{% set flag = 0 %}
{% for tag in tasks.tags %}
    {% if tag.id == "20" %}
        {% set flag = 1 %}
    {% endif %}
{% endfor %}
{{ flag }}

Problem

To simply check if an array contains a certain value I would do: ``` {% if myVar in someOtherArray|keys %} ... {% endif %} ``` However, my array is multi-dimensional. ``` $tasks = array( 'someKey' => 'someValue', ... 'tags' => array( '0' => array( 'id' => '20', 'name' => 'someTag', ), '1' => array( 'id' => '30', 'name' => 'someOtherTag', ), ), ); ``` What i would like is to be able to check if the `$tasks['tags']` has tag id 20. I hope I'm not confusing you by using the PHP array format.

Original source

Related problems