Notice: With the launch of Adobe Cookbooks, this site will no longer be accepting new entries or posting new content. Thanks to everyone who submitted content!

How do I find a value in an array?

ColdFusion does not provide a built-in way to search an array for values. However, you have a few options. You can convert the array to a list using arrayToList(). Once you have done that, you can use listFind(), listFindNoCase(), listContains(), or listContainsNoCase() to search the array.

You can also find UDFs at CFLib that will search an array. You can find both an arrayFind() as well as an arrayFindNoCase() .


This question was written by Raymond Camden.
It was last updated on January 18, 2006 at 3:27:07 PM EST.

CFML Referenced

ArrayToList()
ListContains()
ListFind()
ListFindNoCase()
ListContainsNoCase()

Categories

Data Structures

Comments

Comment made by Larry C. Lyons on January 18, 2007 at 8:32 AM
Actually there is a way to do an array find in CF using CF without converting the array to a list or other array manipulation.

According to a recent blog by Rupesh Kumar of Adobe (http://coldfused.blogspot.com/), ColdFusion arrays are an implementation of java lists (java.util.List). So all the Java list methods are available for CF arrays.

So to search an array all you need to do is add the appropriate method. For example, given this array:

<cfset arry = listToArray("tom, dick, harry, phred")>

You can do a find like this: <cfset findValue = arry.indexOf("harry")>

would return a 2 - the index value of harry in the array.

indexOf returns the index value of the item in the array.

Just remember the Java methods are case sensitive, so Harry or hArry etc., would not be found. A second caution is that the index values start at 0 in Java arrays not 1 as in CF.


Comment made by Raymond Camden on January 18, 2007 at 9:26 AM
Good point Larry, however, in general I try to stick to CFML for the answers here. (In general. :)


Comment made by Larry C. Lyons on January 23, 2007 at 3:21 PM
I thought CFMX was Java. Or at least that's what the Adobe reps keep telling us. ;)


Comment made by Raymond Camden on January 23, 2007 at 3:37 PM
Heh, well, true, but for the audience here, I'm trying to stick to CFML as much as possible.


Comment made by Matt on May 14, 2007 at 8:38 AM
I've tried this but searching for a dynamic variable, and the function always returns -1 even if the value exists in the array


Comment made by Raymond Camden on May 14, 2007 at 9:15 AM
It may be in how you are calling it. Perhaps you can send me your code directly and I'll take a look.


Comment made by izbug on August 4, 2008 at 6:37 PM
It works fine for me, just a quick note, do not use it for bool comparison, since not found or (-1) in CFML will evaluate to true. Instead use <cfif myArray.indexOf('something') neq -1> returns true if found.</cfif> when searching for string 'something'


Comment made by izbug on August 4, 2008 at 6:45 PM
Ironically the suggestion at top of this page to use a UDF, from cflib.org, actually uses the indexOf method itself.


Comment made by lchan on November 4, 2008 at 2:25 PM
indexOf() doesn't work if you are using a multidimensional array. The UDFs won't work either, because they're using indexOf(). It seems CF handles the multidimensionality by nesting unidimensional arrays (call cfdump to see this), and the search does not look below the first level where all it sees are a bunch of arrays.