10

I know I can use an array value in double quotation. Like this:

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

But when I use Multidimensional array, I can`t see my result:

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

Here, the output is: he is Array[1]

What`s the reason?

And I know I can use my code like this:

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

But I don't want this.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Majid Sadr
  • 911
  • 7
  • 18
  • 2
    Is `family` a const? – Alon Eitan Jun 27 '16 at 12:52
  • 2
    The default representation of array in a string goes one level deep only. So, if you have the items themselves as arrays, it does not go further to parse them. – Mohit Bhardwaj Jun 27 '16 at 12:52
  • 3
    Read the docs: http://php.net/manual/en/language.types.string.php#language.types.string.parsing – axiac Jun 27 '16 at 12:58
  • Possible duplicate of [Interpolation (double quoted string) of Associative Arrays in PHP](http://stackoverflow.com/questions/4738850/interpolation-double-quoted-string-of-associative-arrays-in-php) – Jack jdeoel Jun 27 '16 at 13:30

2 Answers2

8

You should enclose more complicated structures in curly braces:

echo "he is {$twoDimArr['family'][1]}";
wazelin
  • 721
  • 11
  • 21
8

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

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


See String parsing documentation and echo() function for more details (especially Example #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
tektiv
  • 14,010
  • 5
  • 61
  • 70