PHP - 多维数组



多维数组是数组的数组。在 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"]
];

遍历二维数组

需要两个嵌套循环来遍历二维数组中的所有元素。foreach 循环更适合数组遍历。二维数组就像数据在行和列中的表格表示。

示例

以下示例显示了如何以表格形式重现二维数组:

<?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";
   }
?>

它将生成以下输出

  1    2    3    4
 10   20   30   40
100  200  300  400

示例

我们还可以使用两个嵌套的foreach循环来遍历二维关联数组。将外部数组的每一行解包到行键和行值变量中,并使用内部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";
   }
?>

它将生成以下输出

row1
key11 => val11  key12 => val12  key13 => val13
row2
key21 => val21  key22 => val22  key23 => val23
row3
key31 => val31  key32 => val32  key33 => val33

访问二维数组中的元素

访问和修改数组中元素的 $arr[$key] 语法也可以扩展到二维数组。对于二维索引数组,可以使用表达式“$arr[$i][$j]”获取和分配第 i 行第 j 个元素。

示例

<?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]);
?>

它将生成以下输出

Value at [2], [2] :300

同样,第 i 行第 j 列的值可以设置为另一个值。

$tbl[2][2] = 250;

示例

如果是二维关联数组,我们需要使用所需列的行键和键值变量来访问或修改其值。

<?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"];
?>

它将生成以下输出

value at row2 - key22 is val22

多维数组

在上面的示例中,我们有一个数组,其中每个键的关联值是另一个键值对的集合,我们称之为二维数组。这个概念可以扩展到任意数量的级别。例如,如果内部数组中的每个元素将其键与另一个数组关联,则它成为一个三维数组。

这是一个三维数组的示例

$arr3D = [
   [
      [1, 0, 9],
      [0, 5, 6],
      [1, 0, 3]
   ],
   [
      [0, 4, 6],
      [0, 0, 1],
      [1, 2, 7]
   ],
];

示例

要遍历这样的三维数组,我们需要三个嵌套的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";
   }
?>

它将生成以下输出

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";
}

示例

让我们将上述三维数组$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);
?>

它将生成以下输出

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

此递归函数可用于任何类型的数组,无论是索引数组还是关联数组,以及任何维度。

示例

让我们使用二维关联数组作为 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);
?>

它将生成以下输出

key11 => val11  key12 => val12  key13 => val13
key21 => val21  key22 => val22  key23 => val23
key31 => val31  key32 => val32  key33 => val33
广告