espresso_coffee
espresso_coffee

Reputation: 6110

How to loop through 2 dimensional array?

I have two dimensional array that looks like this after I used to output this on the screen:

array
1   
array
1   0600
2   
array
1   0
3   
array
1   0600
4   
array
1   0
5   
array
1   0615
6   
array
1   0
7   
array
1   0615
8   
array
1   0
9   
array
1   0630
10  
array
1   0
11  
array
1   0630
12  
array
1   1 

So I want to loop through my array and output values in this order:

0600   0
0600   0
0615   0
0615   0
0630   0  
0630   1

Here is the code how I created my Array:

<cfloop list="#ListGetAt(dataList,i,",")#" index="z" delimiters="|">
    <cfoutput query="getR" group="theYear">
        <cfset name = myArray.append([z])>
        <cfif Description eq z>
            <cfset count = myArray.append([theCount])>
        <cfelse>
            <cfset count = myArray.append([0])>
        </cfif>
    </cfoutput>
</cfloop>

So how I can loop now to get this two records in order as I showed you above? I tried this but that did not work:

<cfoutput>
    <cfloop from="1" to="#arraylen(myArray)#" index="i">
        <cfloop array="#myArray#" index="j">
            #i# - #myArray[1][j]#<br/>
        </cfloop>
    </cfloop>
</cfoutput>

Edit: When I used this code:

<cfloop array="#myArray#" index="i"> 
    <cfloop array="#myArray#" index="j"> 
        <cfoutput> #myArray[i][j]#<br/> </cfoutput> 
    </cfloop> 
</cfloop> 

I got this error:

The value coldfusion.runtime.Array cannot be converted to a number.

If anyone can help with this problem please let me know.

Upvotes: 0

Views: 3571

Answers (2)

Bonanza
Bonanza

Reputation: 1621

You can fix your loop by:

<cfoutput>
<cfloop array="#myArray#" index="firstDimension"> 
    <cfloop array="#firstDimension#" index="secondDimension"> 
        #secondDimension#<br/>
    </cfloop> 
</cfloop>
</cfoutput> 

Upvotes: 2

John Whish
John Whish

Reputation: 3036

This is how you'd get the output from your 2-dimensional array.

<cfscript>
data = [
    ["0600", 0],
    ["0600", 0],
    ["0615", 0],
    ["0615", 0],
    ["0630", 0],
    ["0630", 1]
];

// script version
for (foo in data) {
    writeOutput(foo[1] & " " & foo[2] & "<br>");
}

</cfscript>

<cfoutput>
tag version...<br>
<cfloop array="#data#" index="foo">
    #foo[1]# #foo[2]#<br>
</cfloop>
</cfoutput>

An example of it in use here: http://trycf.com/gist/86b42b56ef7348ec0d44/acf2016?theme=monokai

However, I'm not sure why you're using a 2-dimensional array when an array of structs (key value pairs) seems an easier way to do it:

For example:

<cfscript>
data = [
    {key:"0600", count:0},
    {key:"0600", count:0},
    {key:"0615", count:0},
    {key:"0615", count:0},
    {key:"0630", count:0},
    {key:"0630", count:1}
];

// script version
for (foo in data) {
    writeoutput(foo.key & " " & foo.count & "<br>");
}

</cfscript>

<cfoutput>
tag version...<br>
<cfloop array="#data#" index="foo">
    #foo.key# #foo.count#<br>
</cfloop>
</cfoutput>

Upvotes: 4

Related Questions