从带有结构的数组中获取数组密钥和结构数据

发布于 2025-02-04 04:17:49 字数 1083 浏览 3 评论 0原文

我有一个具有结构的数组。问题是我无法在同一循环中输出数组编号和结构数据,但是如果我有两个不同的循环,则数据将不会出现任何错误。

下面的代码有两个不同的循环,显示了我如何分别访问数组编号和结构数据。

如何在同一循环中输出数组编号和结构数据?

可以在此处测试代码 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尛丟丟 2025-02-11 04:17:49

简化您的代码,并添加本地范围(假设您在功能中),这也是另一种方法:

<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>

Simplifying your code a bit, and adding local scope (assuming you're in a function), here's another way to do this, too:

<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>
画离情绘悲伤 2025-02-11 04:17:49

只是弄清楚了。

<cfloop array ="#arryRe#" item="item" index="i">

    <cfoutput>
        #i# #item.id# #item.title#<br />
    </cfoutput>
  
</cfloop>

Just figured it out.

<cfloop array ="#arryRe#" item="item" index="i">

    <cfoutput>
        #i# #item.id# #item.title#<br />
    </cfoutput>
  
</cfloop>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文