PHP Multidimensional arrays cannot be in double quotes

I know that I can use the value of the array in double quote. Like this:

<?php echo "my name is: $arr[name]"; ?>

But when I use a multidimensional array, I can not see the result:

<?php echo "he is $twoDimArr[family][1]"; ?>

Here's the conclusion: it is an array [1]

What is the reason?

And I know that I can use my code as follows:

<?php echo "he is ".$twoDimArr[family][1]; ?>

But I do not want this.

+4
source share
2 answers

You should attach more complex structures in braces:

echo "he is {$twoDimArr['family'][1]}";
+4
source

You should do something like this using curly braces {and }:

echo "he is {$twoDimArr['family'][1]}";


. String parsing documentation echo() . ( # 1):
// You can also use arrays  
$baz = array("value" => "foo");

echo "this is {$baz['value']} !"; // this is foo !

// Using single quotes will print the variable name, not the value  
echo 'foo is $foo'; // foo is $foo
+3

All Articles