0

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!

3
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by blocblue
2

How about:

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

    foreach($elements as $element) {
        echo "{$element[0]} | {$element[1] | {$element[2]}<br />";
    }
}
Votes + Comments
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)
                )
            );
Votes + Comments
Thanks for the 1st part of the code. The codes work!
0

@blocblue

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

0

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

0

@blocblue
@Biim

It Works!!

3

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.