在一行代码中回显一些字符串和多维数组元素 [英] Echo some string and multidimensional array element in one line of code

查看:81
本文介绍了在一行代码中回显一些字符串和多维数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在为php做一些奇怪的事情.

I've been struggling with some weird thing about php for a while.

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

It seems as if it is unable to display some string and a multidimensional array element using one line of code.

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

For example we have a simple 3d array:

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

现在,如果我想显示一些字符串和第3级元素,则必须执行以下操作:

Now if I want to display some string and that 3rd level element, I'll have to do something like this:

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

或者这个:

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

那么有什么方法可以只用一行代码来实际完成它? 谢谢!

So is there any way to actually do it in just one line of code? Thank you!

推荐答案

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

PHP.Net

复杂(卷曲)语法

之所以不称其为复杂"是因为其语法很复杂,而是因为它允许使用复杂的表达式.

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:

这篇关于在一行代码中回显一些字符串和多维数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆