0

I'm trying to create a simple 2D array in PHP and it doesn't seem to work as described. For instance, I tried the sample code from the w3schools.com site:

$cars = array
  (
  array("Volvo",100,96),
  array("BMW",60,59),
  array("Toyota",110,100)
  );

But when I call:

 echo "$cars[1][1]";

it outputs "Array[1]", not "60". As far as I can tell, the only thing that's getting stored is the string "Array". It doesn't matter how big or small the array is or what method I declare it in or whether it's string or integer, etc... it doesn't actually store the proper data in any sort of array format.

4

1 回答 1

2

您的问题在于您回显元素的方式。

echo $cars[1][1];    //60 

您在变量周围使用引号:

echo "$cars[1][1]"; // Array[1]

如果您使用花括号,则可以将数组包含在带引号的字符串中:

echo "{$cars[1][1]}";    // 60
于 2013-09-06T00:02:13.507 回答