我知道我可以在双引号中使用数组值,就像这样:
<?php echo "my name is: $arr[name]"; ?>
但是当我使用多维数组时,我无法看到我的结果:
<?php echo "he is $twoDimArr[family][1]"; ?>
这里的输出是:he is Array[1]
为什么会这样呢?
我知道可以像这样使用我的代码:
<?php echo "he is ".$twoDimArr[family][1]; ?>
但我不想要这个。
在编写更为复杂的结构时,请使用花括号进行封闭:
echo "he is {$twoDimArr['family'][1]}";
{
和 }
:echo "he is {$twoDimArr['family'][1]}";
字符串解析
文档和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
family
是一个常量吗? - Alon Eitan