Coldfusion将阵列分成两部分

Zac*_*Zac 5 arrays coldfusion cfquery

我可以将数组拆分成两个独立的数组,原始数组中的每个元素用":"分隔吗?":"之前的文本转到array1,":"之后的文本转到array2

<cfset tempArr = DeserializeJSON(URL.data) />
<cfset selectList = "" />
    <cfloop array=#tempArr# index="i">
<cfset selectList = listappend(selectList,i) />
</cfloop>
Run Code Online (Sandbox Code Playgroud)

现在,这段代码抓住了整个元素,而不是分开.

编辑

示例字符串将是:

FIRST_NAME:鲍勃


first_name进入selectList1 Bob进入selectList2


宏伟的计划同样会有其他领域:

FIRST_NAME:鲍勃

姓氏:Shmo

年龄:27

等等...

编辑:答案

通过使用代码解决了问题

<!---Variables--->
<cfset temp1 = "" />
<cfset temp2 = "" />
<cfset selectList1 = "" /><!---Holds column names for tables--->
<cfset selectList2 = "" /><!---Holds query parameters for column names. Ie,
                                    values for use in the WHERE clause--->

<cfloop array=#tempArr# index="i"><!---Loop through contents of the array--->
    <cfset temp1 = GetToken(i,1,":")/><!---Store the column name--->
    <cfset temp2 = GetToken(i,2,":")/><!---Query parameter--->

    <cfset selectList1 = listAppend(selectList1, temp1)/><!---Adds to list of column names--->
    <cfset selectList2 = listAppend(selectList2, temp2)/><!---Adds to the list of query parameters--->
</cfloop>
Run Code Online (Sandbox Code Playgroud)

wil*_*mbq 9

我想如果没有看到你的数组示例,你的意思是将数组中的数据拆分为两个列表?

<cfset selectList1 = listAppend(selectList1, listFirst(i,':')) >
<cfset selectList2 = listAppend(selectList2, listLast(i,':')) >
Run Code Online (Sandbox Code Playgroud)

  • *CF通常会以0*No开始迭代,大多数事情都是基于1的. (4认同)
  • +1我刚刚输入了类似的答案.你打败了我. (2认同)
  • 仅供参考:另一种选择是使用`getToken`.它比`listFirst/Last`的优点是你不需要验证长度.如果给定的列表元素不存在,`getToken`返回一个空字符串.在这种情况下,如果它少于两个元素. (2认同)