Jump to content

[SOLVED] Array dereferencing


awrobinson

Recommended Posts

I'm new to php though I'm pretty experienced with tcl and to a lesser extent perl. I'm working with multidimensional arrays. However, apparently I don't get how to handle them. Here is an example:

 

<?php

$myarray = array(

    0 => array( 0 => "zero", 1 => "one", 2 => "two" ),

    1 => array( 0 => "red", 1 => "green", 2 => "blue" ),

    2 => array( 0 => "how", 1 => "do", 2 => "you" )

);

 

for ($i = 0; $i < 3; $i++ ) {

    for ($j = 0; $j < 3; $j++) {

        print "myarray[ $i ][ $j ]: $myarray[$i][$j]";

    }

}

?>

 

When I run it, I'm expecting to get

 

myarray[ 0 ][ 0 ]: zero

myarray[ 0 ][ 1 ]: one

myarray[ 0 ][ 2 ]: two

myarray[ 1 ][ 0 ]: red

myarray[ 1 ][ 1 ]: green

myarray[ 1 ][ 2 ]: blue

myarray[ 2 ][ 0 ]: how

myarray[ 2 ][ 1 ]: do

myarray[ 2 ][ 2 ]: you

 

What I get instead is

 

myarray[ 0 ][ 0 ]: Array[0]

myarray[ 0 ][ 1 ]: Array[1]

myarray[ 0 ][ 2 ]: Array[2]

myarray[ 1 ][ 0 ]: Array[0]

myarray[ 1 ][ 1 ]: Array[1]

myarray[ 1 ][ 2 ]: Array[2]

myarray[ 2 ][ 0 ]: Array[0]

myarray[ 2 ][ 1 ]: Array[1]

myarray[ 2 ][ 2 ]: Array[2]

 

What don't I understand?

 

Thanks!

 

Andrew Robinson

 

Link to comment
https://forums.phpfreaks.com/topic/127007-solved-array-dereferencing/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.