PDA

View Full Version : easy multidimensional array problem



james438
09-13-2008, 05:18 AM
I can't seem to get the following to work:

<?php
$ar[0][0]="mess";
echo"$ar[0][0]";
?>

My end goal is to try to turn an array into a multidimensional array. Say I have an array

$ar=array(me ss, w e, as df, e d);

I want to explode it into a multidimensional array where $ar[0][0]=me and $ar[0][1]=ss.

Any ideas on these two problems?

james438
09-13-2008, 05:35 AM
<?php
$ar=array('me ss','w e','as df','e d');
$ar[0]=explode(' ',$ar[0]);
print_r($ar);
?>

The above will turn an array into a multidimensional array.

Still don't know why
<?php
$ar[0][0]="mess";
echo"$ar[0][0]";
?> is not working.

print($arry[0][0]); works though. I'll probably figure it out quickly enough. Just lazy tonight I guess.

EDIT: for some reason removing the quotes in the echo allows the echo to work just fine.

Twey
09-13-2008, 10:43 AM
Adding quotes around a variable where they are not needed is pointlessly inefficient. You've already got the value you want, but then you're creating a string and interpolating the value into it — in order to get back the same value.

In this case, it didn't work because array-style variable access is not supported in double quotes by default. You have to wrap it in braces to distinguish it from the rest of the string: print "{$ar[0][0]}";.