awrobinson Posted October 4, 2008 Share Posted October 4, 2008 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 More sharing options...
Barand Posted October 4, 2008 Share Posted October 4, 2008 Use {} when printing complex array elements inside strings print "myarray[ $i ][ $j ]: {$myarray[$i][$j]}<br/>"; Link to comment https://forums.phpfreaks.com/topic/127007-solved-array-dereferencing/#findComment-657011 Share on other sites More sharing options...
awrobinson Posted October 4, 2008 Author Share Posted October 4, 2008 Thanks! Link to comment https://forums.phpfreaks.com/topic/127007-solved-array-dereferencing/#findComment-657014 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.