Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-07T10:01:10.710Z
Answers.Text: Thanks @Jamie I think I will need to create a plugin as it's not working on what I require it for. Cheers
Answers.UserDisplayName: Lettie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-07T09:42:11.383Z
Answers.LastActivityDate: 2016-04-07T09:42:11.383Z
Answers.UserDisplayName: Jamie Pittock
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-02-10T14:24:26.637Z
Answers.Text: 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)
Answers.UserDisplayName: ianp
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-10-06T14:14:57.220Z
Answers.LastActivityDate: 2021-10-06T14:14:57.220Z
Answers.UserDisplayName: Parthiban
Body: <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>
  
Tags: <templating><array>
Title: Is there a way to check if a value exists in an array in twig?
ThreadText: # 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](http://twig.sensiolabs.org/doc/tests/iterable.html) 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](https://craftcms.com/docs/plugins/variables) and doing it directly in PHP.
  
  > **Comments:**
  > **username_0**: Thanks @username_1 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)  
  
  ---
  
  