Php 简明教程
PHP - Multidimensional Array
多维数组是数组的数组。在 PHP 数组中,每个元素可以是另一个数组。如果数组由值或键值对组成,且值属于标量的单数类型,则它是一维数组。如果数组中的每个元素都是一个或多个标量值组成的数组,则它是二维数组。
A multidimensional array is an array of arrays. In a PHP array, each element can be another array. If the array consists of values or key-value pairs with values being of singular scalar types, it is a one-dimensional array. If each element in an array is an array of one or more scalar values, it is a two-dimensional array.
PHP 数组也可能是一个二维关联数组,其中外层数组的每个元素都是键值对,而该值又是另一个关联数组。
A PHP array may be a two-dimensional associative array also, where each element of the outer array is key-value pair, the value being another associative array.
# one dimensional indexed array
$arr = [10, 20, 30, 40];
# one dimensional associative array
$arr = ["key1"=> "val1", "key2" => "val2", "key3" => "val3"];
# two dimensional indexed array
$arr = [
[1,2,3,4],
[10, 20, 30, 40],
[100, 200, 300, 400]
];
# two dimensional associative array
$arr = [
"row1" => ["key11" => "val11", "key12" => "val12", "key13" => "val13"],
"row2" => ["key21" => "val21", "key22" => "val22", "key23" => "val23"],
"row3" => ["key31" => "val31", "key32" => "val32", "key33" => "val33"]
];
Iterating over a 2D Array
遍历二维数组中的所有元素需要两个嵌套的循环。 foreach 循环更适合数组遍历。二维数组类似于行和列中数据的表格形式表示。
Two nested loops will be needed to traverse all the elements in a 2D array. The foreach loop is more suitable for array traversal. A 2D array is like a tabular representation of data in rows and columns.
Example
以下示例展示了如何以表格形式重现二维数组 −
The following example shows how you can reproduce a 2D array in a tabular form −
<?php
$tbl = [
[1,2,3,4],
[10, 20, 30, 40],
[100, 200, 300, 400]
];
echo ("\n");
foreach ($tbl as $row){
foreach ($row as $elem){
$val = sprintf("%5d", $elem);
echo $val;
}
echo "\n";
}
?>
它将生成以下 output −
It will produce the following output −
1 2 3 4
10 20 30 40
100 200 300 400
Example
我们也可以用两个嵌套的 foreach 循环来遍历二维关联数组。在 row-key 和 row-value 变量中解包外层数组的每一行,并用内层 foreach 循环遍历每一行元素。
We can also employ two nested foreach loops to traverse a 2D associative array. Unpack each row of the outer array in row-key and row-value variables and traverse each row elements with the inner foreach loop.
<?php
$tbl = [
"row1" => ["key11" => "val11", "key12" => "val12", "key13" => "val13"],
"row2" => ["key21" => "val21", "key22" => "val22", "key23" => "val23"],
"row3" => ["key31" => "val31", "key32" => "val32", "key33" => "val33"]
];
echo ("\n");
foreach ($tbl as $rk=>$rv){
echo "$rk\n";
foreach ($rv as $k=>$v){
echo "$k => $v ";
}
echo "\n";
}
?>
它将生成以下 output −
It will produce the following output −
row1
key11 => val11 key12 => val12 key13 => val13
row2
key21 => val21 key22 => val22 key23 => val23
row3
key31 => val31 key32 => val32 key33 => val33
Accessing the Elements in a 2D Array
访问和修改数组中元素的 $arr[$key] 语法也可以扩展到二维数组。对于二维索引数组,可以使用表达式 “@ {s7}” 获取和分配第 i 行的第 j 个元素。
The $arr[$key] syntax of accessing and modifying an element in the array can be extended to a 2D array too. For a 2D indexed array, the jth element in the ith row can be fetched and assigned by using the expression "$arr[$i][$j]".
Example
<?php
$tbl = [[1,2,3,4], [10, 20, 30, 40], [100, 200, 300, 400]];
# prints number in index 2 of the row 2
print ("Value at [2], [2] :" . $tbl[2][2]);
?>
它将生成以下 output −
It will produce the following output −
Value at [2], [2] :300
类似地,可以将第 i 行和第 j 列的值设置为另一个值。
Similarly, the value at ith row and jth column may be set to another value.
$tbl[2][2] = 250;
Example
如果它是一个二维关联数组,我们需要使用所需列的行键和键值变量来访问或修改其值。
If it is a 2D associative array, we need to use the row key and key-value variables of the desired column to access or modify its value.
<?php
$tbl = [
"row1" => ["key11" => "val11", "key12" => "val12", "key13" => "val13"],
"row2" => ["key21" => "val21", "key22" => "val22", "key23" => "val23"],
"row3" => ["key31" => "val31", "key32" => "val32", "key33" => "val33"]
];
print "value at row2 - key22 is " . $tbl["row2"]["key22"];
?>
它将生成以下 output −
It will produce the following output −
value at row2 - key22 is val22
Multi-dimensional Array
在上面的示例中,我们有了一个数组,其中每个键的关联值是另一组键值对集合,我们称之为二维数组。该概念可以扩展到任意多个级别。例如,如果内层数组中的每个元素将其键与另一个数组关联,则它将变成一个三维数组。
In the above example, we had an array in which the associated value of each key was another collection of key-value pairs, and we call it as a 2D array. The concept can be extended to any number of levels. For example, if each element in the inner array associates its key to another array, it becomes a three-dimensional array.
以下是 example 三维数组的示例 −
Here is an example of a three-dimensional array −
$arr3D = [
[
[1, 0, 9],
[0, 5, 6],
[1, 0, 3]
],
[
[0, 4, 6],
[0, 0, 1],
[1, 2, 7]
],
];
Example
要遍历这样的三维数组,我们需要三个嵌套的 foreach 循环,如下所示 −
To traverse such a 3D array, we need three nested foreach loops, as shown below −
<?php
$arr3D = [
[[1, 0, 9],[0, 5, 6],[1, 0, 3]],
[[0, 4, 6],[0, 0, 1],[1, 2, 7]],
];
foreach ($arr3D as $arr) {
foreach ($arr as $row) {
foreach ($row as $element) {
echo "$element ";
}
echo "\n";
}
echo "\n";
}
?>
它将生成以下 output −
It will produce the following output −
1 0 9
0 5 6
1 0 3
0 4 6
0 0 1
1 2 7
然而,完全有可能声明一个扩展到任意维度的数组。为此,我们需要一个通用的解决方案来遍历任意维度的数组。
However, it is entirely possible to declare an array extending upto any number of dimensions. For that we need to have a generalized solution to traverse an array of any dimensions.
Recurve Traversal of Multidimensional Array
以下代码展示了一个递归函数,如果某个键的值是另一个数组,它会自己调用自己。如果我们向此函数传递任何数组作为参数,它将被遍历,显示其中的所有 k-v 对。
The following code shows a recursive function that calls itself if the value of a certain key is another array. If we pass any array as an argument to this function, it will be traversed, showing all the k-v pairs in it.
function showarray($arr) {
foreach ($arr as $k=>$v) {
if (is_array($v)) {
showarray($v);
} else {
echo "$k => $v ";
}
}
echo "\n";
}
Example
让我们将上面的三维数组 $arr3D 传递给它并查看结果 −
Let us pass the above 3D array $arr3D to it and see the result −
<?php
$arr3D = [
[[1, 0, 9],[0, 5, 6],[1, 0, 3]],
[[0, 4, 6],[0, 0, 1],[1, 2, 7]],
];
function showarray($arr){
foreach ($arr as $k=>$v){
if (is_array($v)){
showarray($v);
} else {
echo "$k => $v ";
}
}
echo "\n";
}
showarray($arr3D);
?>
它将生成以下 output −
It will produce the following output −
0 => 1 1 => 0 2 => 9
0 => 0 1 => 5 2 => 6
0 => 1 1 => 0 2 => 3
0 => 0 1 => 4 2 => 6
0 => 0 1 => 0 2 => 1
0 => 1 1 => 2 2 => 7
此递归函数可用于任何类型的数组,无论是索引数组还是关联数组,还可以用于任何维度。
This recursive function can be used with any type of array, whether indexed or associative, and of any dimension.
Example
让我们使用一个二维关联数组作为参数来展示 showarray() 函数 −
Let us use a 2D associative array as argument to showarray() function −
<?php
$tbl = [
"row1" => ["key11" => "val11", "key12" => "val12", "key13" => "val13"],
"row2" => ["key21" => "val21", "key22" => "val22", "key23" => "val23"],
"row3" => ["key31" => "val31", "key32" => "val32", "key33" => "val33"]
];
function showarray($arr){
foreach ($arr as $k=>$v){
if (is_array($v)){
showarray($v);
} else {
echo "$k => $v ";
}
}
echo "\n";
}
showarray($tbl);
?>
它将生成以下 output −
It will produce the following output −
key11 => val11 key12 => val12 key13 => val13
key21 => val21 key22 => val22 key23 => val23
key31 => val31 key32 => val32 key33 => val33