Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ColdFusion: How to check if a certain element exists in a 2-dimensional array?

Tags:

I have an 2 dimensional array. I write 3 values into the array during its initialization and i add a fourth value if an array value is not equal with a value passed through a 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 this error:

You have attempted to dereference a scalar variable of type class coldfusion.runtime.Array as a structure with members.

like image 402
mrt181 Avatar asked Jun 25 '09 14:06

mrt181


1 Answers

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

<cfif ArrayLen(matarray[i]) gte 4>
      ,status = 'true'
</cfif>
like image 93
Soldarnal Avatar answered Sep 30 '22 09:09

Soldarnal