Arrays
在PHP中一个数组实际上是一个有次序的映射。一个映射是映射值到关键字上。这个类型在单独的方法上被优化的,你可以作为一个真实的数组或一个列表(向量),hashtable (一个映射的执行),字典,聚集,堆栈,队列和更多的来使用它。因为你可能还有另外的PHP-数组作为一个值,你也可以十分容易的模仿树结构。 数据挖掘研究院
这个结构的解释超过了这本手册的范围,但是你将发现为这结构的最小的范例。关于这个结构的更多信息,请你查阅其它文献。 数据挖掘研究院
语法
用array()指定数组
一个数组可以被array() 构造。它由一对key => value并用逗号分割的一系列的号码组成。 数据挖掘研究院
一个 key 是任意的非负整数或一个字符串组成。 如果一个是由一个标准的非负整数表达的,它将被解释成这样(i.e. "8" 将被解释成8,"08" 将被解释成"08").
一个值可以是任意的。
忽略键。如果你忽略一个键,那么新键将用最大的整数索引加一。如果整数索引也不存在,这个键将是0。如果你已经指定一个值给一个键,那么这个将被复盖。
array( [key =>] value
, ...
)
// 键是任意的字符串或非负整数
// 值可以是任意的 |
Array do"s and don"ts
为什么$foo[bar]是错误的?
在旧的脚本中你可能看到过下边的语法:
这是错误的,但它会工作。然而,为什么是错误的呢?在这之后的syntax 片段中规定,表达式必须在方括号之间。。这意味着你可以象下边一样做: 这个例子使用一个函数的返回值作为数组的索引。PHP也知道是常量,你可以见E_*。
$error_descriptions[E_ERROR] = "A fatal error has occured"; $error_descriptions[E_WARNING] = "PHP issued a warning"; $error_descriptions[E_NOTICE] = "This is just an informal notice"; |
$error_descriptions[1] = "A fatal error has occured"; $error_descriptions[2] = "PHP issued a warning"; $error_descriptions[8] = "This is just an informal notice"; 数据挖掘研究院 |
Then, how is it possible that $foo[bar] works? It works, because bar is due to its syntax expected to be a constant expression. However, in this case no constant with the name bar exists. PHP now assumes that you meant bar literally, as the string "bar", but that you forgot to write the quotes.
So why is it bad then?
At some point in the future, the PHP team might want to add another constant or keyword, and then you get in trouble. For example, you already cannot use the words empty and default this way, since they are special keywords.
And, if these arguments don"t help: this syntax is simply deprecated, and it might stop working some day. 数据挖掘研究院
Tip: When you turn error_reporting to E_ALL, you will see that PHP generates warnings whenever this construct is used. This is also valid for other deprecated "features". (put the line error_reporting(E_ALL); in your script)
Note: Inside a double-quoted string, an other syntax is valid. See variable parsing in strings for more details.
Examples
The array type in PHP is very versatile, so here will be some examples to show you the full power of arrays. 数据挖掘研究院
// this
$a = array( "color" => "red"
, "taste" => "sweet"
, "shape" => "round"
, "name" => "apple"
, 4 // key will be 0
);
// is completely equivalent with
$a["color"] = "red";
$a["taste"] = "sweet";
$a["shape"] = "round";
$a["name"] = "apple";
$a[] = 4; // key will be 0
$b[] = "a";
$b[] = "b";
$b[] = "c";
// will result in the array array( 0 => "a" , 1 => "b" , 2 => "c" ),
// or simply array("a", "b", "c") 数据挖掘研究院 |
Note that it is currently not possible to change the values of the array directly in such a loop. A workaround is the following:
|
Example 6-6. Collection
|
This example creates a one-based array.
Arrays are ordered. You can also change the order using various sorting-functions. See array-functions for more information. 数据挖掘研究院
Because the value of an array can be everything, it can also be another array. This way you can make recursive and multi-dimensional arrays.

