php 数组的简写:是否有像 {} 或 [] 这样的文字语法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4271874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Shorthand for arrays: is there a literal syntax like {} or []?
提问by James
What is the shorthand for array notation in PHP?
PHP 中数组表示法的简写是什么?
I tried to use (doesn't work):
我尝试使用(不起作用):
$list = {};
It will be perfect, if you give links on some information about other shorthands for PHP.
如果您提供有关 PHP 其他速记的一些信息的链接,那将是完美的。
回答by Alin Purcaru
Update:
As of PHP 5.4.0 a shortened syntaxfor declaring arrays has been introduced:
更新:
从 PHP 5.4.0开始,引入了用于声明数组的缩短语法:
$list = [];
Previous Answer:
上一个答案:
There isn't. Only $list = array();But you can just start adding elements.
没有。只有$list = array();但您可以开始添加元素。
<?php
$list[] = 1;
$list['myKey'] = 2;
$list[42] = 3;
It's perfectly OK as far as PHP is concerned. You won't even get a E_NOTICE for undefined variables.
就 PHP 而言,这完全没问题。对于未定义的变量,您甚至不会得到E_NOTICE。
E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array.
E_NOTICE 级别错误是在使用未初始化变量的情况下发出的,但在将元素附加到未初始化数组的情况下则不会。
As for shorthand methods, there are lots scattered all over. If you want to find them just read The Manual.
至于速记方法,到处都是。如果您想找到它们,只需阅读手册。
Some examples, just for your amusement:
一些例子,仅供您娱乐:
$arr[]shorthand forarray_push.- The
foreachconstruct echo $string1, $string2, $string3;- Array concatenation with
+ - The existence of
elseif - Variable embedding in strings,
$name = 'Hyman'; echo "Hello $name";
$arr[]的简写array_push。- 该
foreach结构 echo $string1, $string2, $string3;- 数组连接
+ - 的存在
elseif - 字符串中的变量嵌入,
$name = 'Hyman'; echo "Hello $name";
回答by AgelessEssence
YES, it exists!!
是的,它存在!!
Extracted from another Stack Overflow question:
摘自另一个 Stack Overflow 问题:
The shortened syntax for arrays has been rediscussed, accepted, and is now on the way be released with PHP 5.4
数组的缩短语法已经过重新讨论和接受,现在正在与 PHP 5.4 一起发布
Usage:
用法:
$list = [];
Reference: PHP 5.4 Short Hand for Arrays
参考:PHP 5.4 数组简写
回答by Dieter Gribnitz
It is also possible to define content inside [ ] like so:
也可以在 [ ] 中定义内容,如下所示:
$array = ['vaue1', 'value2', 'key3'=>['value3', 'value4']];
This will only work in php5.4 and above.
这仅适用于 php5.4 及更高版本。
回答by Matthew
回答by StasM
回答by Daniel Vandersluis
回答by user3231257
You can declare your array as follows:
您可以按如下方式声明数组:
$myArray1 = array(num1, num2, num3);
$myArray2 = array('string1', 'string2', 'string3');
$myArray3 = array( 'stringkey1'=>'stringvalue1', 'stringkey2'=>'stringvalue2');
$myArray4 = array( 'stringkey1'=>numValue1, 'stringkey2'=>numValue2);
$myArray5 = array( numkey1=>'stringvalue1', numkey2=>'stringvalue2');
$myArray6 = array( numkey1=>numValue1, numkey2=>numValue2);
You can have as many embedded arrays as you need.
您可以根据需要拥有任意数量的嵌入式数组。
回答by Damien Golding
I just explode strings into an array like so:
我只是将字符串分解为一个数组,如下所示:
$array = explode(",","0,1,2,3,4,5,6,7,8,9,10");

