[英]Get array key and structure data from an array with structure
I've got an array that has a structure.我有一个具有结构的数组。 Problem is that I can't output the array number and the structure data in the same loop, but if I have two different loops, the data will show without any errors.
问题是我不能在同一个循环中 output 数组编号和结构数据,但是如果我有两个不同的循环,数据将显示没有任何错误。
The code below has two different loops showing how I can access the array number and structure data separately.下面的代码有两个不同的循环,展示了我如何分别访问数组编号和结构数据。
How do you output the array number and structure data in the same loop?你如何将output的数组编号和结构数据放在同一个循环中?
Code can be tested here https://cffiddle.org/代码可以在这里测试https://cffiddle.org/
<!--- Create new structure --->
<cfset structRe = StructNew() />
<!--- Add data to structure --->
<cfset structRe.id = "14">
<cfset structRe.title = "Title">
<!--- Create new Array --->
<cfset arryRe = ArrayNew(1) />
<!--- Add structure to array --->
<cfset ArrayAppend(arryRe, structRe)>
<cfoutput>
<cfdump var="#arryRe#" />
</cfoutput>
<!--- Loop to access structure --->
<cfloop array ="#arryRe#" index="i">
<cfoutput>
#i.id# #i.title# <br />
</cfoutput>
</cfloop>
<!--- Loop to access array number --->
<cfloop array ="#arryRe#" item="item" index="i">
<cfoutput>
#i# <br />
</cfoutput>
</cfloop>
Simplifying your code a bit, and adding local scope (assuming you're in a function), here's another way to do this, too:稍微简化您的代码,并添加本地 scope(假设您在函数中),这也是执行此操作的另一种方法:
<cfset local.arrayRe = [
{"id": "14", "title": "Title Fourteen"}
, {"id": "15", "title": "Title Fifteen"}
] />
<cfloop array="#local.arrayRe#" index="local.i" item="local.stItem">
<cfoutput>
Item ##: #local.i#, ID: #local.stItem.id#, Title: #local.stItem.title#<br />
</cfoutput>
</cfloop>
Just figured it out.刚刚想通了。
<cfloop array ="#arryRe#" item="item" index="i">
<cfoutput>
#i# #item.id# #item.title#<br />
</cfoutput>
</cfloop>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.