ArrayContains()

Returns the position of the first element in the array that matches the item that we are searching for, or 0 if the item is not found.

ArrayContains( haystack=array, needle=object, substringMatch=boolean );

Returns: Number

Argument Description
haystack
array, required

The array in which to search for the item

Alias: array, arr

needle
object, required

The item that we are looking for in the array

Alias: object, obj, o

substringMatch
boolean, optional

If set to true then a substring match will also return an array position. This will only work with simple values. Passing true with complex objects will throw an exception.

Usage Notes

Lucee returns the index of the match, while ACF returns a Boolean, both of which are truthy

Examples

numbers = [ 4, 3, 2, 1 ];
	dump(var=numbers, label="numbers");
<span class="nv">positionOfThree</span> <span class="o">=</span> <span class="nf">ArrayContains</span><span class="p">(</span> <span class="nv">numbers</span><span class="p">,</span> <span class="m">3</span><span class="p">);</span>
<span class="nf">echo</span><span class="p">(</span><span class="s2">&quot;Position of 3: &quot;</span> <span class="o">&amp;</span> <span class="nv">positionOfThree</span> <span class="o">&amp;</span> <span class="s2">&quot;&lt;br&gt;&quot;</span><span class="p">);</span> <span class="c">// outputs 2</span>
<span class="nv">words</span> <span class="o">=</span> <span class="p">[</span> <span class="s1">&#39;hello&#39;</span> <span class="p">,</span> <span class="s1">&#39;world&#39;</span> <span class="p">];</span>
<span class="nf">dump</span><span class="p">(</span><span class="k">var</span><span class="o">=</span><span class="nv">words</span><span class="p">,</span> <span class="nv">label</span><span class="o">=</span><span class="s2">&quot;Words&quot;</span><span class="p">);</span>
<span class="nv">positionOfWorld</span> <span class="o">=</span> <span class="nf">ArrayContains</span><span class="p">(</span> <span class="nv">words</span><span class="p">,</span> <span class="s1">&#39;world&#39;</span> <span class="p">);</span>
<span class="nv">positionOfSubstring</span> <span class="o">=</span> <span class="nf">ArrayContains</span><span class="p">(</span> <span class="nv">words</span><span class="p">,</span> <span class="s1">&#39;el&#39;</span><span class="p">,</span> <span class="nv">true</span> <span class="p">);</span> <span class="c">// substring matching</span>
<span class="nf">echo</span><span class="p">(</span><span class="s2">&quot;Position of substring &#39;el&#39;: &quot;</span> <span class="o">&amp;</span> <span class="nv">positionOfSubstring</span> <span class="o">&amp;</span> <span class="s2">&quot;&lt;br&gt;&quot;</span> <span class="p">);</span> <span class="c">// outputs 1</span>
<span class="nf">echo</span><span class="p">(</span><span class="s2">&quot;Position of &#39;World&#39;: &quot;</span> <span class="o">&amp;</span> <span class="nv">positionOfWorld</span><span class="p">);</span> <span class="c">// outputs 2</span>

See also