PHP双引号下的多维数组

今天在VSCode上用PHP处理JSON文件的时候遇到了,VSCode 给出了提示,但是没有注意到.

虽然用其他的方式绕过了,但是总是想知道原因. 在stackoverflow上发现了原因:

https://stackoverflow.com/questions/38054476/how-to-put-multidimensional-arrays-double-quoted-strings

PHP在双引号下输出一维数组,没有问题:

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

但是输出二维甚至多维数组的时候就出现了问题:

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

输出为:

he is Array[1]

在php官网的文档中发现了答案:

http://php.net/manual/en/language.types.string.php#language.types.string.parsing

一维数组属于simple variable,二维甚至多维数组属于complex variable,需要使用curly braces(大括号)包围起来

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

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.