Phi*_*hil 2 arrays coldfusion coldfusion-10
我正在使用CF 10.当一个脚本正在运行时,我正在创建一个包含代表单个车辆的不同值的数组.我正在使用我的脚本顶部初始化数组
<cfset myArray = ArrayNew(1)>
然后,当我运行我的脚本时,我正在使用...
<cfset temp = ArrayAppend(myArray, myQuery.VIN)>
这一切都很好,但我想要做的是在到达脚本中的每个部分之后,我想更新当前部分查询中的任何VINS,以便它们是值数组.那个阵列是..
[1] ["VIN NUMBER 123"] [2] ["VIN NUMBER 456"]
成为...
[1] ["VIN NUMBER 123"] ["VALUE1"] ["VALUE2"] ["VALUE3"] [2] ["VIN NUMBER 456"] ["VALUE2"]
我以为我可以这样做......
<cfset vindex = ArrayFind(myArray,vinToFind)>
<cfif NOT IsArray('myArray[vindex]')>
     <cfset myArray[vindex] = ArrayNew(1)>
</cfif>
<cfset temp = ArrayAppend(myArray[vindex],valueToAppend)>
但最后,我的阵列仍然是一维的.我究竟做错了什么?
我推荐@ale建议的一组数组.
<cfset myArray = ArrayNew(1)>
<!--- check if the VIN is already present --->
<cfset vindex = ArrayFind(myArray, vinToFind)>
<!--- the VIN was found --->
<cfif (vindex gt 0)>
    <!--- if the VIN is still on its own, transform it to an array --->
    <cfif NOT IsArray(myArray[vindex])>
        <cfset temp = myArray[vindex]> <!--- remember current VIN --->
        <cfset myArray[vindex] = ArrayNew(1)> <!--- transform present index to an array --->
        <cfset ArrayAppend(myArray[vindex], temp)> <!--- add VIN back in --->
    </cfif>
    <!--- add the VIN --->
    <cfset ArrayAppend(myArray[vindex], valueToAppend)>
<!--- VIN is not present yet --->
<cfelse>
    <cfset ArrayAppend(myArray, valueToAppend)>
</cfif>
以下是一些提示:
ArrayAppend(1)可写成[].ArrayAppend(myArray, value)可写成myArray.add(value).ArrayAppend因为它总是会返回true.跟着去吧<cfset ArrayAppend(myArray, value)>.IsArray期望变量,而不是字符串.IsArray("myArray")将会返回false时IsArray(myArray)返回true.上面的代码使用了数组文字和add方法.
<cfset myArray = []>
<!--- check if the VIN is already present --->
<cfset vindex = arrayFind(myArray, vinToFind)>
<!--- the VIN was found --->
<cfif (vindex gt 0)>
    <!--- if the VIN is still on its own, transform it to an array --->
    <cfif not isArray(myArray[vindex])>
        <cfset myArray[vindex] = [ myArray[vindex] ]> <!--- transform present index to an array --->
    </cfif>
    <!--- add the VIN --->
    <cfset myArray[vindex].add(valueToAppend)>
<!--- VIN is not present yet --->
<cfelse>
    <cfset myArray.add(valueToAppend)>
</cfif>