Php 简明教程
PHP - Arrays
数组是一个数据结构,它在一个变量中存储一个或多个数据值,它们之间存在某种关系。例如,如果你想存储一个班级里 10 个学生的成绩,那么你可以定义一个长度为 10 的数组,而不是定义 10 个不同的变量。
An array is a data structure that stores one or more data values having some relation among them, in a single variable. For example, if you want to store the marks of 10 students in a class, then instead of defining 10 different variables, it’s easy to define an array of 10 length.
PHP 中的数组的行为与 C 中的数组略有不同,因为 PHP 是一个动态类型语言,而 C 是一个静态类型语言。
Arrays in PHP behave a little differently than the arrays in C, as PHP is a dynamically typed language as against C which is a statically type language.
-
An array in PHP is an ordered map that associates values to keys.
-
A PHP array can be used to implement different data structures such as a stack, queue, list (vector), hash table, dictionary, etc.
-
The value part of an array element can be other arrays. This fact can be used to implement tree data structure and multidimensional arrays.
在 PHP 中声明数组有两种方法。一种是使用内置 array() 函数,另一种是使用更短的语法,其中数组元素放在方括号内。
There are two ways to declare an array in PHP. One is to use the built-in array() function, and the other is to use a shorter syntax where the array elements are put inside square brackets.
The array() Function
内置 array() 函数使用提供给它的参数并返回数组类型对象。一个或多个以逗号分隔的参数是数组中的元素。
The built-in array() function uses the parameters given to it and returns an object of array type. One or more comma-separated parameters are the elements in the array.
array(mixed ...$values): array
括号中的每个值可以是单个值(可能是数字、字符串、任何对象,甚至另一个数组),或键值对。键与其值之间的关联由“⇒”符号表示。
Each value in the parenthesis may be either a singular value (it may be a number, string, any object or even another array), or a key-value pair. The association between the key and its value is denoted by the "⇒" symbol.
Using Square Brackets [ ]
除了 array() 函数之外,还可以将以逗号分隔的数组元素置于方括号内以声明数组对象。在这种情况下,元素也可以是单个值、字符串或另一个数组。
Instead of the array() function, the comma-separated array elements may also be put inside the square brackets to declare an array object. In this case too, the elements may be singular values or a string or another array.
$arr1 = [10, "asd", 1.55, true];
$arr2 = ["one"=>1, "two"=>2, "three"=>3];
$arr3 = [ [10, 20, 30],
["Ten", "Twenty", "Thirty"],
["physics"=>70, "chemistry"=>80, "maths"=>90] ];
Types of Arrays in PHP
有三种不同种类的数组,并且每个数组值都使用一个 ID(称为数组索引)访问。
There are three different kind of arrays and each array value is accessed using an ID which is called the array index.
-
Indexed Array − An array which is a collection of values only is called an indexed array. Each value is identified by a positional index staring from "0". Values are stored and accessed in linear fashion.
-
Associative Array − If the array is a collection of key-value pairs, it is called as an associative array. The key component of the pair can be a number or a string, whereas the value part can be of any type. Associative arrays store the element values in association with key values rather than in a strict linear index order.
-
Multi Dimensional Array − If each value in either an indexed array or an associative array is an array itself, it is called a multi dimensional array. Values are accessed using multiple indices
NOTE - 内置数组函数在函数引用 PHP Array Functions 中给出。
NOTE − Built-in array functions is given in function reference PHP Array Functions
值得注意的是,PHP 内部将上述任何类型都视为关联数组本身。对于索引数组(其中每个值具有索引),则索引本身就是其键。var_dump() 函数揭示了这一事实。
It may be noted that PHP internally considers any of the above types as an associative array itself. In case of an indexed array, where each value has index, the index itself is its key. The var_dump() function reveals this fact.
Example
在此示例中, arr1 是一个索引数组。但是,var_dump()(显示任何对象的结构化信息)显示每个值都具有索引作为其键。
In this example, arr1 is an indexed array. However, var_dump()which displays the structured information of any object, shows that each value is having its index as its key.
<?php
$arr1 = [10, "asd", 1.55, true];
var_dump($arr1);
?>
它将生成以下 output −
It will produce the following output −
array(4) {
[0]=>
int(10)
[1]=>
string(3) "asd"
[2]=>
float(1.55)
[3]=>
bool(true)
}
Example
对于多维索引数组也适用相同的原则,其中数组中的每个值都是另一个数组。
The same principle applies to a multi-dimensional index array, where each value in an array is another array.
<?php
$arr1 = [
[10, 20, 30],
["Ten", "Twenty", "Thirty"],
[1.1, 2.2, 3.3]
];
var_dump($arr1);
?>
它将生成以下 output −
It will produce the following output −
array(3) {
[0]=>
array(3) {
[0]=>
int(10)
[1]=>
int(20)
[2]=>
int(30)
}
[1]=>
array(3) {
[0]=>
string(3) "Ten"
[1]=>
string(6) "Twenty"
[2]=>
string(6) "Thirty"
}
[2]=>
array(3) {
[0]=>
float(1.1)
[1]=>
float(2.2)
[2]=>
float(3.3)
}
}
Accessing the Array Elements
要从给定的数组访问任何元素,可以使用数组[键] 语法。
To access any element from a given array, you can use the array[key] syntax.
Example
对于索引数组,将索引放到方括号中,因为索引本身就是键。
For an indexed array, put the index inside the square bracket, as the index itself is anyway the key.
<?php
$arr1 = [10, 20, 30];
$arr2 = array("one"=>1, "two"=>2, "three"=>3);
var_dump($arr1[1]);
var_dump($arr2["two"]);
?>
它将生成以下 output −
It will produce the following output −
int(20)
int(2)
我们将在后续章节中更详细地探讨 PHP 数组的类型。
We shall explore the types of PHP arrays in more details in the subsequent chapters.