Now that we have a handle on the concept of strings, let’s take a look at the power and flexibility of arrays. Arrays are known as compound data types; all that really means is that they are more complex in structure than simple strings and integers, which are also known as scalar data types. Imagine an array as an egg carton. It carries 12 individual compartments that can house one egg each, yet it travels around as one entity. The compartments can be broken off and made into single egg holders, or holders in any number combination. Additionally, these compartments are not limited to holding only eggs; they can hold rocks, or cookies, or match sticks. Of course, an analogy like this has its limitations—egg cartons cannot easily be expanded and they cannot hold other egg cartons, all of which, we will see, arrays are excellent at doing.
说完字符串的概念,现在再让我们来看看强大而又极具灵活性的数组。数组被称为复合数据类型,意思是说,它比简单的字符串或数字以及其他已知的数据类型要复杂一些。你可以把数组想像成一个存放鸡蛋的纸盒(市场里能看到有很多槽的那种,我们一般叫蛋托)。但是呢,盒里的每个鸡蛋可以分出很多更小的槽,而这些小小的槽可以拆除或者再放进更多的鸡蛋筐。此外,这些槽不仅能放鸡蛋,还能放面包、饼干、火腿肠什么的。当然,这个比喻其实并不恰当,因为真正的纸盒不能随意缩放,也不可能再在槽里装进去另一个纸盒。我之所以这么说,是想表达一个意思:数组真的很好很强大。
Let’s talk more precisely about arrays. Like egg cartons, arrays have compartments (elements) that hold data. The elements and their respective data always travel together (although you can have an empty element without data) and are known as key/value pairs. So, if we had an array of five elements, each containing a number in the range from 1 to 5, it would look like Table 5-1 (elements start their counting with 0).
我们来更详细的谈谈数组。前面说了,它像蛋盒,有很多可以放数据的槽(元素)。这些元素和其中的数据总是一一对应(虽然你也可以让元素空着不放数据),这就人们所说的键/值对。因此,如果我们有一个包含5个元素的数组,每个分别包含从数字1到5,就像表 5-1所示(元素是从0开始计数的)
Table 5-1. Visual representation of an array with numeric (indexed) keys
表 5-1 形象化的表示数字(作索引)的数组
键 0 1 2 3 4
值 1 2 3 4 5
Indexed Arrays
Arrays with numerical keys are known as indexed arrays. The keys can also be named with strings, if you prefer, creating what is known as an associative array, as you will see later in this chapter. Let’s consider an array called $myArray.
索引数组
用数字作为键名的数组一般叫索引数组(译者注:这时的键又叫下标)。当然只要你愿意,键也可以用字符串表示,那样的话,指的就是在本章稍后看到的关联数组。让我们考虑一个变量名为 $myArray 的数组。
Array variable names follow the same naming rules as regular PHP var- iables (see the section “Variables: Data Types, Loose Typing, and Scope” on page 9).
数组变量的命名同样遵循PHP变量命名约定(参见第9页 “变量:数据类型,弱类型,作用域” )
In PHP code, you can reference the elements of an array by their keys, surrounded by square brackets. If we want to take the value of the third element of the array—the contents being the number 3 in this case—and assign it to its own variable, the code would look like this:
在PHP语言中,你可以使用方括号括起来的键名来访问数组中的元素。如果我们要取得数组中第三个元素的值——在这个例子里它是数字3——并将它分配给一个要定义的变量,实现的代码应该是这样:
// 切记, 数组元素的键(即下标)从0开始算起
$singleValue = $myArray[2];
echo $singleValue ; // 会输出 3
Now, this assumes that we have already defined the array somewhere else in our code. There are two ways in which to create arrays; the first is a variation on the use of the square bracket syntax. Here is the code for creating an array with this method:
现在,假设我们已经在代码中提前定义了一个数组。有两个办法可以实现,第一个就是用方括号语法,下面是用这个方法的代码:
$myArray[0] = 1;
$myArray[1] = 2;
$myArray[2] = 3;
$myArray[3] = 4;
$myArray[] = 5;
This method assumes that we already know the key order and their values, as the key numbers here are hardcoded. Notice that the last line of code in the above example is not “forcing” the key number inside the square brackets; when this is done (not pro- viding the key number), the result is that the next available integer key will be assigned to that element for us.
此方法假定我们已经知道数组的键和对应的值,但这是种对键的硬编码。注意上面示例代码中最后一行并没有“强制”分配方括号内键的数值,这样做(就是不提供键的具体数值)的结果,使它为我们将分配下一个可用的键索引数值给此元素。
Creating an array in this fashion may or may not be what you want. The other method available is to use the array function with the key/value pairs passed together and separated by commas, like so:
用这种形式创建数组,或许有些啰嗦。另一种更常用方法是将一组用逗号分隔的键/值对传递给array函数。像这样:
$myArray = array(0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5);
This way of creating an array is much more condensed, yet it may be a little more difficult for humans to read.
这种形式更简单明了,但可能会稍稍不利于代码阅读。
If you want to create an empty array, just use the array function without any parameters:
如果你想创建一个空数组,只要写一个空参数的array函数即可:
$a = array();
The keys of any array have to be named uniquely, otherwise there would be confusion when referencing their contents. PHP won’t prevent you from using the same key multiple times in an assignment, but each identical key assigned will replace the one before it.
任何数组的键必须唯一,否则会在由键取内容时造成混乱。PHP不会禁止你为同一个键的内容反复赋值,但是每次赋值时都会覆盖同名键的之前内容。
Associative Arrays
关联数组
So far we have looked at indexed arrays, but as I mentioned before, the key portion of an array can also consist of character strings. The data values that we have looked at so far have also been numerical, but these too can be changed to string data, as shown in Table 5-2.
到目前为止,我们知道了索引数组,但就像我前面提到的,数组的键也可以用字符串来表示;同样的,每个键所对应的值不光可以是数字,当然也可以是字符串,如表 5-2 所示:
键 first second fname initial lname
值 1 2 Peter B MacIntyre
RSS