<p>Like you said, to test if a value exists in a Twig array you'd do:</p>

<pre><code>{% if myValue in ['apples', 'oranges'] %}
    {# do something... #}
{% endif %}
</code></pre>

<p>To check whether the value is an array or object you could use the <a href="http://twig.sensiolabs.org/doc/tests/iterable.html" rel="noreferrer">iterable</a> test:</p>

<pre><code>{% if myValue is iterable %}
    {# myValue is an array or object #}
{% else %}
    {# myValue is probably a string #}
{% endif %}
</code></pre>

<p>To be certain, you might be best coming out of Twig using a plugin <a href="https://craftcms.com/docs/plugins/variables" rel="noreferrer">Template Variable</a> and doing it directly in PHP.</p>

CC BY-SA 3.0
2016-04-07T10:01:10.710Z
Thanks @Jamie I think I will need to create a plugin as it's not working on what I require it for. Cheers
Lettie
CC BY-SA 3.0
2016-04-07T09:42:11.383Z
2016-04-07T09:42:11.383Z
Jamie Pittock
<p>Craft 3.7.15 onwards you can use <code>is array</code> twig test. More info <a href="https://github.com/craftcms/cms/blob/develop/CHANGELOG.md" rel="nofollow noreferrer">https://github.com/craftcms/cms/blob/develop/CHANGELOG.md</a></p>

CC BY-SA 4.0
2023-02-10T14:24:26.637Z
Docs for this now here: https://craftcms.com/docs/4.x/dev/tests.html#array

```
{{ ['oh', 'hello', 'there'] is array ? 'true' : 'false' }}
{# result: true #}
```

(Relevant changelog now here: https://github.com/craftcms/cms/blob/3.8/CHANGELOG.md)
ianp
CC BY-SA 4.0
2021-10-06T14:14:57.220Z
2021-10-06T14:14:57.220Z
Parthiban
<p>Done some searching on this but can't quite find what I'm looking for. Most threads are regarding keys.</p>

<p>I want to know if there is a way to test if a value exists in a twig array.</p>

<p>It should be able to check whether the value is a variable, object or an array.</p>

<p>So for example:</p>

<pre><code>{% if value not in array %}
    .... do someting
{% endif %}
</code></pre>

<p>Any suggestions welcome.</p>

<templating><array>
Is there a way to check if a value exists in an array in twig?
# Is there a way to check if a value exists in an array in twig?

(Asked by: username_0 on 2016-04-07)

Done some searching on this but can't quite find what I'm looking for. Most threads are regarding keys.
I want to know if there is a way to test if a value exists in a twig array.
It should be able to check whether the value is a variable, object or an array.
So for example:
{% if value not in array %}
    .... do someting
{% endif %}

Any suggestions welcome.

**Tags:** templating, array


---

## 2 answers

### Answer by username_1 on 2016-04-07. Score: 10

Like you said, to test if a value exists in a Twig array you'd do:
{% if myValue in ['apples', 'oranges'] %}
    {# do something... #}
{% endif %}

To check whether the value is an array or object you could use the iterable test:
{% if myValue is iterable %}
    {# myValue is an array or object #}
{% else %}
    {# myValue is probably a string #}
{% endif %}

To be certain, you might be best coming out of Twig using a plugin Template Variable and doing it directly in PHP.

> **Comments:**
> **username_0**: Thanks @Jamie I think I will need to create a plugin as it's not working on what I require it for. Cheers  

---

### Answer by username_2 on 2021-10-06. Score: 1

Craft 3.7.15 onwards you can use `is array` twig test. More info <https://github.com/craftcms/cms/blob/develop/CHANGELOG.md>

> **Comments:**
> **username_3**: Docs for this now here: https://craftcms.com/docs/4.x/dev/tests.html#array  ``` {{ ['oh', 'hello', 'there'] is array ? 'true' : 'false' }} {# result: true #} ```  (Relevant changelog now here: https://github.com/craftcms/cms/blob/3.8/CHANGELOG.md)  

---

