Php 简明教程
PHP - Indexed Array
在 PHP 中,数组元素可以是键值对的集合,也可以仅包含值。如果数组仅包含值,则称之为索引数组,因为每个元素都由一个递增的索引标识,从“0”开始。
In PHP, the array elements may be a collection of key-value pairs or it may contain values only. If the array consists of values only, it is said to be an indexed array, as each element is identified by an incrementing index, starting with "0".
可以使用 array() 函数或方括号语法在 PHP 中创建一个索引数组。
An indexed array in PHP may be created either by using the array() function or with the square bracket syntax.
$arr1 = array("a", 10, 9.99, true);
$arr2 = ["a", 10, 9.99, true];
数组中的每个元素都有一个位置索引,第一个元素的索引为“0”。var_dump() 函数揭示了这些数组的结构化信息,如下所示:
Each element in the array has a positional index, the first element being at index "0". The var_dump() function reveals the structured information of these arrays as −
array(4) {
[0]=>
string(1) "a"
[1]=>
int(10)
[2]=>
float(9.99)
[3]=>
bool(true)
}
我们可以使用索引遍历数组,获取给定索引处的值或修改元素的值。
We can use the index to traverse the array, fetch the value at a given index or modify the value of an element in place.
Traversing an Indexed Array in PHP
任何类型的 PHP 循环都可以用来遍历数组。如果我们想使用 for 或 while 循环,我们必须使用 count() 函数找到数组中的元素数量,并将其值用作计数 for 或 while 循环的测试条件。
Any type of PHP loop can be employed to traverse an array. If we want to use a for or while loop, we have to find the number of elements in the array with count() function and use its value as the test condition for the counted for or while loop.
Example
以下代码使用 for 循环列出索引数组中的所有元素。
The following code uses a for loop to list all the elements in an indexed array.
<?php
$numbers = array(10, 20, 30, 40, 50);
for ($i=0; $i<count($numbers); $i++){
echo "numbers[$i] = $numbers[$i] \n";
}
?>
它将生成以下 output −
It will produce the following output −
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
您还可以使用 while 或 do-while 循环遍历索引数组。同样,我们需要使用 count() 函数找到数组长度。
You can also use a while or do-while loop to traverse an indexed array. Here too, we need to find the array length with count() function.
Example
以下代码以相反的顺序遍历给定的索引数组:
The following code traverses the given indexed array in reverse order −
<?php
$numbers = array(10, 20, 30, 40, 50);
$i = count($numbers)-1;
while ($i>=0){
echo "numbers[$i] = $numbers[$i] \n";
$i--;
}
?>
它将生成以下 output −
It will produce the following output −
numbers[4] = 50
numbers[3] = 40
numbers[2] = 30
numbers[1] = 20
numbers[0] = 10
Accessing the Array Elements Using Index
使用 array[index] 语法,可以访问数组中的任何值。特定索引处的可以分配新值。从而对数组进行修改。
You can access any value from an array using the array[index] syntax. The value at a specific index may be assigned with a new value. The array is thus modified in place.
Example
以下程序从数组 $arr1 中获取值,并以相反的顺序将它们放在 $arr2 中,因此 $arr1 中的第 0 个位置的值将变为 $arr2 中的最后一个值。
The following program fetches the values from an array $arr1 and places them in $arr2 in the reverse order. So the value at 0th position in $arr1 becomes the last value in $arr2.
<?php
$arr1 = array(10, 20, 30, 40, 50);
$size = count($arr1);
for ($i=0; $i<$size; $i++){
$arr2[$size-$i-1] = $arr1[$i];
}
for ($i=0; $i<$size; $i++){
echo "arr1[$i] = $$arr1[$i] arr2[$i] = $$arr2[$i] \n";
}
?>
它将生成以下 output −
It will produce the following output −
arr1[0] = $10 arr2[0] = $50
arr1[1] = $20 arr2[1] = $40
arr1[2] = $30 arr2[2] = $30
arr1[3] = $40 arr2[3] = $20
arr1[4] = $50 arr2[4] = $10
Traversing an Indexed Array Using "foreach" Loop
您还可以使用 foreach 来遍历一个索引数组。看看下面的 example −
You can also use the foreach loop to iterate through an indexed array. Take a look at the following example −
<?php
$arr1 = [10, 20, 30, 40, 50];
foreach ($arr1 as $val){
echo "$val \n";
}
?>
它将生成以下 output −
It will produce the following output −
10
20
30
40
50
请注意,PHP 在内部将索引数组视为关联数组,其中索引被视为键。此事实可以通过数组的 var_dump() 输出得到验证。
Note that PHP internally treats the indexed array as an associative array, with the index being treated as the key. This fact can be verified by the var_dump() output of the array.
Example
我们可以使用 foreach 语法在 key 和 value 变量中解包一个索引数组的每个元素 −
We can unpack each element of an indexed array in the key and value variables with foreach syntax −
<?php
$arr1 = [10, 20, 30, 40, 50];
foreach ($arr1 as $key => $val){
echo "arr1[$key] = $val \n";
}
?>
它将生成以下 output −
It will produce the following output −
arr1[0] = 10
arr1[1] = 20
arr1[2] = 30
arr1[3] = 40
arr1[4] = 50
在 PHP 中,一个数组可能只是值和键值对的组合。PHP 只为没有键的值分配索引。
In PHP, an array may be a mix of only values and key-value pairs. PHP assigns the index only to the values without keys.
Example
在这个示例中,PHP 为数字分配了递增索引,跳过出现的键值对。
In this example, PHP assigns incrementing index to the numbers, skipping the key-value pair appearing in it.
<?php
$arr1 = [10, 20,
"vals" => ["ten", "twenty"],
30, 40, 50];
var_dump($arr1);
?>
它将生成以下 output −
It will produce the following output −
array(6) {
[0]=>
int(10)
[1]=>
int(20)
["vals"]=>
array(2) {
[0]=>
string(3) "ten"
[1]=>
string(6) "twenty"
}
[2]=>
int(30)
[3]=>
int(40)
[4]=>
int(50)
}