Array Question
Hello,
Can someone figure this out? When i try to search for "27" it doesn't work, but if i try to search for "15" it seems to work. I'm breaking my head for hours trying to figure out why this isn't working.
What i'm trying to accomplish is if i do a search for "27" it takes that position in the array, then grabs that same position in the second array "arrayt[2][4]" and gives me the value, i know its possible to do something like this in perl with hashes but not sure how i can do this in coldfusion. I'm not sure if there is a better way to do this, but this is the only way i could figure out.
<cfset arrayt = ArrayNew(2)>
<cfset arrayt[1][1] = "15">
<cfset arrayt[1][2] = "19">
<cfset arrayt[1][3] = "01">
<cfset arrayt[1][4] = "27">
<cfset arrayt[2][1] = "111">
<cfset arrayt[2][2] = "112">
<cfset arrayt[2][3] = "113">
<cfset arrayt[2][4] = "114">
<cfset amount = ArrayLen(arrayt)>
<!---
Search a multidimensional array for a value.
@9397041 arrayToSearch Array to search. (Required)
@9397041 valueToFind Value to find. (Required)
@9397041 dimensionToSearch Dimension to search. (Required)
@Return Returns a number.
@7111211 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>
<cfoutput>
#ArrayLen(arrayt[1])#
<cfscript>
writeOutput(arrayFindByDimension(arrayt,"27",1) & "<br>");
</cfscript>
</cfoutput>
