Php 简明教程
PHP - Multidimensional Array
多维数组是数组的数组。在 PHP 数组中,每个元素可以是另一个数组。如果数组由值或键值对组成,且值属于标量的单数类型,则它是一维数组。如果数组中的每个元素都是一个或多个标量值组成的数组,则它是二维数组。
PHP 数组也可能是一个二维关联数组,其中外层数组的每个元素都是键值对,而该值又是另一个关联数组。
# 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 循环更适合数组遍历。二维数组类似于行和列中数据的表格形式表示。
Example
以下示例展示了如何以表格形式重现二维数组 −
<?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 −
1 2 3 4
10 20 30 40
100 200 300 400
Example
我们也可以用两个嵌套的 foreach 循环来遍历二维关联数组。在 row-key 和 row-value 变量中解包外层数组的每一行,并用内层 foreach 循环遍历每一行元素。
<?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 −
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 个元素。
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 −
Value at [2], [2] :300
类似地,可以将第 i 行和第 j 列的值设置为另一个值。
$tbl[2][2] = 250;
Example
如果它是一个二维关联数组,我们需要使用所需列的行键和键值变量来访问或修改其值。
<?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 −
value at row2 - key22 is val22
Multi-dimensional Array
在上面的示例中,我们有了一个数组,其中每个键的关联值是另一组键值对集合,我们称之为二维数组。该概念可以扩展到任意多个级别。例如,如果内层数组中的每个元素将其键与另一个数组关联,则它将变成一个三维数组。
以下是 example 三维数组的示例 −
$arr3D = [
[
[1, 0, 9],
[0, 5, 6],
[1, 0, 3]
],
[
[0, 4, 6],
[0, 0, 1],
[1, 2, 7]
],
];
Example
要遍历这样的三维数组,我们需要三个嵌套的 foreach 循环,如下所示 −
<?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 −
1 0 9
0 5 6
1 0 3
0 4 6
0 0 1
1 2 7
然而,完全有可能声明一个扩展到任意维度的数组。为此,我们需要一个通用的解决方案来遍历任意维度的数组。
Recurve Traversal of Multidimensional Array
以下代码展示了一个递归函数,如果某个键的值是另一个数组,它会自己调用自己。如果我们向此函数传递任何数组作为参数,它将被遍历,显示其中的所有 k-v 对。
function showarray($arr) {
foreach ($arr as $k=>$v) {
if (is_array($v)) {
showarray($v);
} else {
echo "$k => $v ";
}
}
echo "\n";
}
Example
让我们将上面的三维数组 $arr3D 传递给它并查看结果 −
<?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 −
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
此递归函数可用于任何类型的数组,无论是索引数组还是关联数组,还可以用于任何维度。
Example
让我们使用一个二维关联数组作为参数来展示 showarray() 函数 −
<?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 −
key11 => val11 key12 => val12 key13 => val13
key21 => val21 key22 => val22 key23 => val23
key31 => val31 key32 => val32 key33 => val33