我有以下结构,我想要做的是为其中的不同键设置变量(personaID、personaName、userClubList),然后为 userClubList 数组中的值设置变量,但不知道如何按照我的方式工作向下遍历结构,找出它有哪些键,然后它们的键值是什么。

arrays - 从 ColdFusion 中的 Struct/Array 访问和设置变量-LMLPHP
!

有人可以帮帮我吗?

最佳答案

实际上,您已经能够使用此结构并且不需要任何映射到其他变量。通过使用点符号 userAccountInfo.personas 或方括号 userAccountInfo["personas"] 来访问结构。您可以通过链接访问 userAccountInfo.personas[1].userClubList[1].clubAbbr 来访问任何结构深度。通常,您会希望通过使用 structKeyExists(userAccountInfo, "personas")isDefined("userAccountInfo.personas") 来检查所有这些结构成员的存在,尤其是数组(不过不建议使用 isDefined,因为它处理范围的方式松散)。您也可以通过在每个检查 structKeyExists(userAccountInfo.personas[1], "userClubList")structKeyExists(userAccountInfo.personas[1].userClubList[1], "clubAbbr") 等上传递下一个成员来链接这些。

无论如何,为了给您更多指导,以下是我对您的结构的理解:

<!--- test data (the one in your screenshot) --->
<cfset userAccountInfo = {}>
<cfset userAccountInfo.personas = []>
<cfset userAccountInfo.personas[1] = {}>
<cfset userAccountInfo.personas[1].personaId = "850074729">
<cfset userAccountInfo.personas[1].personaName = "IcedTube3">
<cfset userAccountInfo.personas[1].userClubList = []>
<cfset userAccountInfo.personas[1].userClubList[1] = {}>
<cfset userAccountInfo.personas[1].userClubList[1].clubAbbr = "Bel">
<cfset userAccountInfo.personas[1].userClubList[1].clubName = "Bell Ville FC">
<cfset userAccountInfo.personas[1].userClubList[1].established = "1363092161">
<cfset userAccountInfo.personas[1].userClubList[1].lastAccessTime = "1363092161">
<cfset userAccountInfo.personas[1].userClubList[1].platform = "360">
<cfset userAccountInfo.personas[1].userClubList[1].year = "2013">

<!--- let's make sure personas exists and is an array --->
<cfif structKeyExists(userAccountInfo, "personas") and isArray(userAccountInfo.personas)>

    <cfset myPersonas = userAccountInfo.personas> <!--- myPersonas will be an array --->
    <cfloop array="#myPersonas#" index="persona"> <!--- persona is supposed to be a struct --->

        <!--- let's make sure each item in the array is really a struct --->
        <cfif isStruct(persona)>

            <cfoutput>Start reading persona.<br />====================<br /><br /></cfoutput>

            <!--- read personaId --->
            <cfif structKeyExists(persona, "personaId")>
                <cfoutput>personaId: #persona.personaId#<br /></cfoutput>
            <cfelse>
                <cfoutput>personaId: [none]<br /></cfoutput>
            </cfif>

            <!--- read personaName --->
            <cfif structKeyExists(persona, "personaName")>
                <cfoutput>personaName: #persona.personaName#<br /></cfoutput>
            <cfelse>
                <cfoutput>personaName: [none]<br /></cfoutput>
            </cfif>

            <!--- read userClubList (let's make sure userClubList exists in persona and is an array) --->
            <cfif structKeyExists(persona, "userClubList") and isArray(persona.userClubList) and not arrayIsEmpty(persona.userClubList)>

                <cfset myUserClubList = persona.userClubList>
                <cfloop array="#myUserClubList#" index="club"> <!--- club is supposed to be a struct --->

                    <!--- let's make sure each item in the array is really a struct --->
                    <cfif isStruct(club)>

                        <cfoutput><br />Start reading club.<br />--------------------<br /><br /></cfoutput>

                        <!--- read clubAbbr --->
                        <cfif structKeyExists(club, "clubAbbr")>
                            <cfoutput>clubAbbr: #club.clubAbbr#<br /></cfoutput>
                        <cfelse>
                            <cfoutput>clubAbbr: [none]<br /></cfoutput>
                        </cfif>

                        <!--- and so on... --->

                        <cfoutput><br />--------------------<br />Done reading club.<br /><br /></cfoutput>

                    </cfif>

                </cfloop>

            <cfelse>
                <cfoutput>userClubList: [none]<br /></cfoutput>
            </cfif>

            <cfoutput><br />====================<br />Done reading persona.<br /><br /></cfoutput>

        </cfif>

    </cfloop>

</cfif>

结果是...
Start reading persona.
====================

personaId: 850074729
personaName: IcedTube3

Start reading club.
--------------------

clubAbbr: Bel

--------------------
Done reading club.


====================
Done reading persona.

希望这可以帮助。 :)

关于arrays - 从 ColdFusion 中的 Struct/Array 访问和设置变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15363900/

10-09 03:48