ColdFusion:如何检查二维数组中是否存在某个元素?

mrt*_*181 5 coldfusion multidimensional-array data-structures

我有一个二维数组.我在初始化期间向数组写入3个值,如果数组值与通过表单传递的值不相等,则添加第四个值.然后我想检查,如果存在第四个值.

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) />
Run Code Online (Sandbox Code Playgroud)

objDao.cfc更新方法

<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>
Run Code Online (Sandbox Code Playgroud)

我的错误尝试导致此错误:

您试图取消引用类coldfusion.runtime.Array类型的标量变量作为具有成员的结构.

Sol*_*nal 8

我相信你只需要像这样检查数组长度:

<cfif ArrayLen(matarray[i]) gte 4>
      ,status = 'true'
</cfif>
Run Code Online (Sandbox Code Playgroud)