How to reference struct/array 3 levels deep?

I am trying to reference a struct/array and get to the 3rd layer of items. If you look at the below dump, there is one member, and an array of items. What I am trying to output on the page is member.laborcraftrate.rate.

This works fine if I only have one result returned, but if the array has more than one entry I get the error “Array index [2] out of range, array size is [1]”.

What is the best way to reference the 3rd level items in a loop/output?

<cfloop index="i" from="1" to="#ArrayLen(myArray.member)#">
        <cfoutput>
            <option value="#myArray["member"][i]["laborcode"]#">#myArray["member"][i]["laborcraftrate"][i]["rate"]#</option>
        </cfoutput>
</cfloop>    

your problem is that you are reusing the [i] index for the top level, again for the third level

Thanks. Would it be something like this then?

#myArray["member"][i]["laborcraftrate"]["rate"]#

I am still getting errors on it when trying that.

you need to loop over each level independently

psuedo code, but you get the idea

<cfloop index="i" from="1" to="#ArrayLen(myArray.member)#">
    <cfloop index="j" from="1" to="#ArrayLen(myArray.member[i])#">
        <cfloop index="k" from="1" to="#ArrayLen(myArray.member[i][j])#">
        <cfloop>
    <cfloop>
<cfloop>


Ahh thanks. Got it. This worked.

<cfloop array="#myArray.member#" index="member">
    <cfloop array="#member.laborcraft#" index="laborcraft">
        <cfoutput>
            <option value="#laborcraft.laborcode#">#laborcraft.rate#</option>
        </cfoutput>
    </cfloop>
</cfloop>
5 Likes

Just as complement, a suggestion along the lines of your original code:

<!--- Strictly speaking, myArray is not an array but a struct (an associative array) --->
<cfset memberArray=myArray.member>
<cfloop index="i" from="1" to="#arrayLen(memberArray)#">
    <cfset labourCraftRateArray=memberArray[i].labourcraftrate>
    <cfloop index="j" from="1" to="#arrayLen(labourCraftRateArray)#">
        <cfoutput>
            <!--- Used: single quotes within double quotes --->
            <option value="#labourCraftRateArray[j]['laborcode']#">#labourCraftRateArray[j]["rate"]#</option>
        </cfoutput>
    </cfloop>
</cfloop>