简体   繁体   English

PHP数组的简写语法

[英]Shorthand syntax for PHP arrays

Is this valid PHP syntax or just a shorthand for the documentation? 这是有效的PHP语法还是只是文档的简写? Does anybody have the link to a help page describing this notation? 是否有人链接到描述此符号的帮助页面?

$array = [
  [1, 2],
  [3, 4],
];

If you have a look at the code examples in the Unpacking nested arrays with list() section on the foreach help page you will see this possibly new notation, however I can't seem to find any help for this. 如果您查看foreach帮助页面上的“ 使用list()用嵌套列表解开嵌套数组”部分中的代码示例,您将看到这个可能的新符号,但是我似乎找不到任何帮助。

The new syntax [] was added in PHP 5.4.0 . 新语法[]是在PHP 5.4.0中添加的。 Check out the documentation on arrays 查看有关阵列的文档

The page you reference is saying that unpacking the array using list() is new in PHP 5.5, not the short array syntax itself, which was added in PHP 5.4. 您引用的页面是说,使用list()解压缩数组是PHP 5.5中的新增功能,而不是PHP 5.4中添加的short数组语法本身。

PHP 5.5 added the ability to iterate over an array of arrays and unpack the nested array into loop variables by providing a list() as the value. PHP 5.5通过提供list()作为值,增加了对数组数组进行迭代并将嵌套数组解压缩为循环变量的功能。

That is no more than an array of arrays. 那不过是一个数组数组。 These two snippets are identical: 这两个片段是相同的:

$array = [
  [1, 2],
  [3, 4],
];

$array = array(
  array (1, 2),
  array (3, 4),
);

This is documented in the PHP manual page for Arrays and has been possible since PHP 5.4. Array的PHP手册页中对此进行了记录,并且自PHP 5.4起就可以实现。

Why don't you just test the code? 您为什么不只是测试代码? I'm using PHP Version 5.4.7 and it is already valid code. 我正在使用PHP版本5.4.7,它已经是有效的代码。 Example: 例:

$array = [
    [1, 2],
    [3, 4],
];
echo '<pre>';
print_r($array);
echo '</pre>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM