加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 在一行代码中回显一些字符串和多维数组元素

发布时间:2020-12-13 22:09:50 所属栏目:PHP教程 来源:网络整理
导读:我一直在努力解决关于php的一些奇怪的事情. 看起来似乎无法使用一行代码显示一些字符串和多维数组元素. 例如,我们有一个简单的3d数组: $ARRAY = array(‘first’= array(array(‘Hello,World
我一直在努力解决关于php的一些奇怪的事情.

看起来似乎无法使用一行代码显示一些字符串和多维数组元素.

例如,我们有一个简单的3d数组:

$ARRAY = array(‘first’=> array(array(‘Hello,World!’)));

现在,如果我想显示一些字符串和第三级元素,我将不得不这样做:

$a = $ARRAY['first'][0][0];
echo"Some string: $a";

或这个:

echo"Some string: ";
echo($ARRAY['first'][0][0]);

那么有没有办法在一行代码中实际执行此操作?
谢谢!

解决方法

echo "Some string: {$ARRAY['first'][0][0]}";

阅读更多PHP.Net

Complex (curly) syntax

This isn’t called complex because the syntax is complex,but because it allows for the use of complex expressions.

Any scalar variable,array element or object property with a string representation can be included via this syntax. Simply write the expression the same way as it would appear outside the string,and then wrap it in { and }. Since { can not be escaped,this syntax will only be recognised when the $immediately follows the {. Use {$to get a literal {$. Some examples to make it clear:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读