ColdFusion: how to check if any element exists in a two-dimensional array?

I have a two dimensional array. I write 3 values ​​to the array during its initialization, and I add the fourth value if the value of the array is not equal to the value passed through the form. Then I want to check if the fourth value exists.

update.cfm

<cfset array = obj.getArray() /> <cfif not StructIsEmpty(form)> <cfloop collection="#form#" item="key"> <cfif left(key,3) eq "ID_"> <cfset number = listLast(key,"_") /> <cfset value = evaluate(0,key) /> <cfloop index="j" from="1" to="#arrayLen(array)#"> <cfif (array[j][1] eq number) and (array[j][3] neq value)> <cfset array[j][3] = value /> <cfset array[j][4] = "true" /> </cfif> </cfloop> </cfif> </cfloop> <cfset obj = createObject("component", "cfc.Obj").init(arg = form.arg, argarray = array) /> <cfset application.objDao.update(obj) /> 

objDao.cfc update method

 <cfset matarray = arguments.obj.getArray() /> <cfloop index="i" from="1" to="#arrayLen(array)#"> <cfquery name="qUpdateAsset" datasource="#variables.instance.dsn#"> UPDATE table SET value = <cfqueryparam value="#matarray[i][3]#" cfsqltype="cf_sql_integer" /> <!--- This is wrong, how do i check the existence correctly? ---> <cfif StructKeyExists(matarray[i],"4")> ,status = 'true' </cfif> WHERE arg = <cfqueryparam value="#arguments.obj.getArg()#" cfsqltype="cf_sql_smallint" /> AND number = <cfqueryparam value="#matarray[i][1]#" cfsqltype="cf_sql_integer" /> </cfquery> </cfloop> 

My wrong attempt results in an error:

You tried to dereference a scalar variable of type class coldfusion.runtime.Array as a member structure.

+4
source share
2 answers

I believe you just need to check the length of the array like this:

 <cfif ArrayLen(matarray[i]) gte 4> ,status = 'true' </cfif> 
+8
source

Good for beginners, I'm not a big Cold Fusion guy ... but I'm sure you cannot use StructKeyExists in an array, since its not a structure.

Also, have you tried something like

 <cfset testValue = matarray[i][4]> <cfif isDefined testValue> ... </cfif> 
-3
source

Source: https://habr.com/ru/post/1286754/


All Articles