Member Avatar for LastMitch

Hi

I'm having problems trying to echo the array, that I created. I'm trying to create a 3-D array.

Here is the file:

$futures = array(array(array("GC", "Gold", 1,605)
                      (array("SI", "Silver", 27)
                      (array("PL", "Platinum", 1475)
                      (array("HG", "Copper", 3.48)),
                (array(array("CCS", "Corn", 707)
                      (array("SNS", "Soybeans", 1,525)
                      (array("WCS", "Wheat", 846)),
                (array(array("CJ", "Cocoa ", 2,335)
                      (array("KT", "Coffee", 183)
                      (array("TT", "Cotton", 70.56)
                      (array("YO", "Sugar #11", 21.95)));

For ($layer = 0; $layer <4; $layer++){
echo "Layer $layer<BR>";
for ( $row = 0; $row <4; $row++ ){
for ( $column = 0; $column <3; $column++ ){
echo "|" futures[$layer][$row][$column];}
echo "<BR>";}
}

I appreciate any suggestions or any examples will do, for me to see what I did wrong. Thanks!

How about:

foreach($futures as $index => $elements) {
    echo "Layer {$index}<br />";

    foreach($elements as $element) {
        echo "{$element[0]} | {$element[1] | {$element[2]}<br />";
    }
}
commented: Thanks for the 2nd part of code! It worked! +2

and the array is declared wrong:

$futures =  array(
                array(
                    array("GC", "Gold", 1605)
                    ,array("SI", "Silver", 27)
                    ,array("PL", "Platinum", 1475)
                    ,array("HG", "Copper", 3.48)
                ),
                array(
                    array("CCS", "Corn", 707)
                    ,array("SNS", "Soybeans", 1.525)
                    ,array("WCS", "Wheat", 846)
                ),
                array(
                    array("CJ", "Cocoa ", 2,335)
                    ,array("KT", "Coffee", 183)
                    ,array("TT", "Cotton", 70.56)
                    ,array("YO", "Sugar #11", 21.95)
                )
            );
commented: Thanks for the 1st part of the code. The codes work! +2
Member Avatar for LastMitch

@blocblue

Thanks for the reply! I will replace the code I have to your and test it out. Thanks for the code.

Member Avatar for LastMitch

@Biim

Thanks for the reply! You still have a good eye! I will also make some adjustments and replace the arrays I have to yours and test it out with blocblue code. Thanks for the code!

Member Avatar for LastMitch

@blocblue
@Biim

It Works!!

Member Avatar for LastMitch

@blocblue
@Biim

I want to "Thanks" both of you for helping me to understand about the 3-D arrays and I got a chance to see how it works and it came out great! I saw all 3 layers! I appreciate both of you taking your time add the code and to answer my question. Thanks!

You're welcome. Have a good weekend!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.