PHP精粹 第五章 数组 (试译片断)

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:


// 切记, 数组元素的键(即下标)从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:


$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:


$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.

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

Comments are closed.