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.

Examples

numbers = [ 4, 3, 2, 1 ];
	dump(var=numbers, label="numbers");
	positionOfThree = ArrayContains( numbers, 3);
	echo("Position of 3: " & positionOfThree & "<br>"); // outputs 2
	words = [ 'hello' , 'world' ];
	dump(var=words, label="Words");
	positionOfWorld = ArrayContains( words, 'world' );
	positionOfSubstring = ArrayContains( words, 'el', true ); // substring matching
	echo("Position of substring 'el': " & positionOfSubstring & "<br>" ); // outputs 1
	echo("Position of 'World': " & positionOfWorld); // outputs 2

See also