从带有结构的数组中获取数组密钥和结构数据
我有一个具有结构的数组。问题是我无法在同一循环中输出数组编号和结构数据,但是如果我有两个不同的循环,则数据将不会出现任何错误。
下面的代码有两个不同的循环,显示了我如何分别访问数组编号和结构数据。
如何在同一循环中输出数组编号和结构数据?
可以在此处测试代码 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>
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.
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?
Code can be tested here 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简化您的代码,并添加本地范围(假设您在功能中),这也是另一种方法:
Simplifying your code a bit, and adding local scope (assuming you're in a function), here's another way to do this, too:
只是弄清楚了。
Just figured it out.