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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.