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.
Accessing multidimensional array elements: There are mainly two ways to access multidimensional array elements in PHP. Elements can be accessed using dimensions as array_name['first dimension']['second dimension']. Elements can be accessed using for loop. Elements can be accessed using for each loop.
A multidimensional array is an array containing one or more arrays.
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 }}
this one is more like a multidimensional loop in case it's necessary
{% for animals in array %}
{% set dogs = animals.dogs %}
{% for dog in dogs %}
{{ dump(dog.type) }}
{% endfor%}
{% endfor %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With