CFLib.org – Common Function Library Project

ArrayFindByDimension(arrayToSearch, valueToFind, dimensionToSearch)

Last updated September 23, 2004

author

Grant Szabo

Version: 1 | Requires: CF6 | Library: DataManipulationLib

Description:
Extends Nathan Dintenfass' ArrayFindNoCase() by adding ability to search a specified dimension of a multidimensional array. Returns numeric array position of first dimension if element found, otherwise returns 0.

Return Values:
Returns a number.

Example:

<cfscript>
arr = arrayNew(2);
arr[1][1] = "apple";
arr[1][2] = "pear";
arr[1][3] = "orange";
arr[2][1] = "star";
arr[2][2] = "nova";
arr[2][3] = "moon";

writeOutput(arrayFindByDimension(arr,"star",1) & "<br>");            
writeOutput(arrayFindByDimension(arr,"star",2) & "<br>");            

</cfscript>

Parameters:

Name Description Required
arrayToSearch Array to search. Yes
valueToFind Value to find. Yes
dimensionToSearch Dimension to search. Yes

Full UDF Source:

<!---
 Search a multidimensional array for a value.
 
 @param arrayToSearch      Array to search. (Required)
 @param valueToFind      Value to find. (Required)
 @param dimensionToSearch      Dimension to search. (Required)
 @return Returns a number. 
 @author Grant Szabo (grant@quagmire.com) 
 @version 1, September 23, 2004 
--->
<cffunction name="ArrayFindByDimension" access="public" returntype="numeric" output="false">
    <cfargument name="arrayToSearch" type="array" required="Yes">
    <cfargument name="valueToFind" type="string" required="Yes">
    <cfargument name="dimensionToSearch" type="numeric" required="Yes">
    <cfscript>
        var ii = 1;
        
        //loop through the array, looking for the value
        for(; ii LTE arrayLen(arguments.arrayToSearch); ii = ii + 1){
            //if this is the value, return the index
            if(NOT compareNoCase(arguments.arrayToSearch[ii][arguments.dimensionToSearch], arguments.valueToFind))
                return ii;
        }
        //if we've gotten this far, it means the value was not found, so return 0
        return 0;
    </cfscript>
</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson