【问题标题】:Iterate through every single key and value in a multi-dimensional associative array PHP遍历多维关联数组 PHP 中的每个键和值
【发布时间】:2019-03-01 15:44:40
【问题描述】:

我刚刚了解了键/值对。

我试图寻找一个现有的答案,并试图了解我可以了解键/值对和关联数组。虽然这变得有点太耗时了。

我无法弄清楚如何迭代这个多维关联数组而不会出现任何错误。

$arr = array(
  'test1' => array(
    'testing 1-1' => array('testing 1-1-1', 'testing 1-1-2', 'testing 1-1-3'),
    'testing 1-2' => array('testing 1-2-1', 'testing 1-2-2', 'testing 1-2-3'),
    'testing 1-3' => array('testing 1-3-1', 'testing 1-3-2', 'testing 1-3-3')),
  'test2' => array(
    'testing 2-1' => array('testing 2-1-1', 'testing 2-1-2', 'testing 2-1-3'),
    'testing 2-2' => array('testing 2-2-1', 'testing 2-2-2', 'testing 2-2-3'),
    'testing 2-3' => array('testing 2-3-1', 'testing 2-3-2', 'testing 2-3-3')),
  'test3' => array(
    'testing 3-1' => array('testing 3-1-1', 'testing 3-1-2', 'testing 3-1-3'),
    'testing 3-2' => array('testing 3-2-1', 'testing 3-2-2', 'testing 3-2-3'),
    'testing 3-3' => array('testing 3-3-1', 'testing 3-3-2', 'testing 3-3-3')));

所以我知道 print_r 可以完美地渲染整个数组,但我想要实现的是能够获取每个可能的键和值。

我尝试通过 foreach 循环逐个回显所有字符串,但似乎单词数组本身会以某种方式导致错误。我相信这是我最合乎逻辑的方法。

foreach ($arr as $test) {
  echo $test . '<br>';
    foreach ($test as $testing1) {
      echo '&nbsp&nbsp' . $testing1 . '<br>';
      foreach ($testing1 as $testing2) {
        echo '&nbsp&nbsp&nbsp&nbsp' . $testing2 . '<br>';
    }
  }
}

虽然它会导致以下结果:

Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 88
Array

Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
  Array
    testing 1-1-1
    testing 1-1-2
    testing 1-1-3

Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
  Array
    testing 1-2-1
    testing 1-2-2
    testing 1-2-3

Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
  Array
    testing 1-3-1
    testing 1-3-2
    testing 1-3-3

Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 88
Array

Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
  Array
    testing 2-1-1
    testing 2-1-2
    testing 2-1-3

Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
  Array
    testing 2-2-1
    testing 2-2-2
    testing 2-2-3

Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
  Array
    testing 2-3-1
    testing 2-3-2
    testing 2-3-3

Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 88
Array

Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
  Array
    testing 3-1-1
    testing 3-1-2
    testing 3-1-3

Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
  Array
    testing 3-2-1
    testing 3-2-2
    testing 3-2-3

Notice: Array to string conversion in /opt/lampp/htdocs/projects/form/phptest.php on line 90
  Array
    testing 3-3-1
    testing 3-3-2
    testing 3-3-3

所以我想知道我是否缺少某些东西或者这是不可能实现的?

更新:感谢您的回答。

这是我根据得到的答案使用的代码:

  foreach ($arr as $k => $test) {
    echo $k . '<br>';
      foreach ($test as $k1 => $testing1) {
        echo '&nbsp&nbsp' . $k1 . '<br>';
        foreach ($testing1 as $k2 => $testing2) {
          echo '&nbsp&nbsp&nbsp&nbsp' . $testing2 . '<br>';
      }
    }
  }

更新:

我真的很喜欢 HoldOffHunger 的建议。

<?php

    // Your Array Here

$arr = [...];

    // IterateArray() function

function IterateArray($data) {
    if(is_array($data)) {
        foreach ($data as $data_key => $data_value) {
            print("\n\n");
            print($data_key . ' :: ');
            print(IterateArray($data_value));
        }
    } else {
        print($data);
        print("\n");
    }
}

    // Call IterateArray()

IterateArray($arr);

?>

不幸的是,我对这个递归函数没有用处。对每个循环实例进行硬编码让我可以为每个嵌套循环做不同的事情。虽然(如果我错了,请纠正我)如果多维数组的每个维度都重复相同的代码,这将很有用。

【问题讨论】:

    标签: php multidimensional-array foreach key-value


    【解决方案1】:

    The foreach statement 可以选择只迭代值,就像你正在做的那样,或者键和值。我怀疑这就是你要找的东西:

    foreach ($arr6 as $k=>$test) {
      echo $k . '<br>';
        foreach ($test as $k1=>$testing1) {
          echo '&nbsp&nbsp' . $k1 . '<br>';
          foreach ($testing1 as $k2=>$testing2) {
            echo '&nbsp&nbsp&nbsp&nbsp' . $testing2 . '<br>';
        }
      }
    }
    

    您收到的通知是因为您尝试在数组上使用echo,这是不可能的。

    【讨论】:

    • 感谢 Miken32,这几乎正是我想要的!
    • 很高兴为您提供帮助!
    【解决方案2】:

    欢迎使用 stackoverflow。 那些东西是注意事项,你不应该对数组使用 echo,而是使用 print_r($ar) 或 var_dump($ar)

    foreach ($arr6 as $test) {
      print_r($test);
        foreach ($test as $testing1) {
          print_r($testing1);
          foreach ($testing1 as $testing2) {
            print_r($testing2);
        }
      }
    }
    

    您也可以像这样打印键:

    foreach ($arr6 as $mykey => $test) {
      echo $mykey;
      print_r($test);
    

    【讨论】:

      【解决方案3】:
         $arr = array(
        'test1' => array(
          'testing 1-1' => array('testing 1-1-1', 'testing 1-1-2', 'testing 1-1-3'),
          'testing 1-2' => array('testing 1-2-1', 'testing 1-2-2', 'testing 1-2-3'),
          'testing 1-3' => array('testing 1-3-1', 'testing 1-3-2', 'testing 1-3-3')),
        'test2' => array(
          'testing 2-1' => array('testing 2-1-1', 'testing 2-1-2', 'testing 2-1-3'),
          'testing 2-2' => array('testing 2-2-1', 'testing 2-2-2', 'testing 2-2-3'),
          'testing 2-3' => array('testing 2-3-1', 'testing 2-3-2', 'testing 2-3-3')),
        'test3' => array(
          'testing 3-1' => array('testing 3-1-1', 'testing 3-1-2', 'testing 3-1-3'),
          'testing 3-2' => array('testing 3-2-1', 'testing 3-2-2', 'testing 3-2-3'),
          'testing 3-3' => array('testing 3-3-1', 'testing 3-3-2', 'testing 3-3-3')));
          foreach ($arr as $key=>$val){
              echo $key;
              foreach($val as $k=>$v){
                  echo $k;
                  foreach($v as $ku=>$vu){
                      echo $ku;    
                  }
              }
          }
      

      你是在回显数组而不是字符串。

      【讨论】:

      • 谢谢 shakogele。这也非常接近我想要的。
      【解决方案4】:

      欢迎来到 SO!

      我想发布一个适用于任何类型数组的解决方案,无论是 3-d、4-d 还是任何维度。那么,为什么不递归foreach()is_array()

      IDEOne: Online Demo/Sandox of IterateArray()

      <?php
      
          // Your Array Here
      
      $arr = [...];
      
          // IterateArray() function
      
      function IterateArray($data) {
          if(is_array($data)) {
              foreach ($data as $data_key => $data_value) {
                  print("\n\n");
                  print($data_key . ' :: ');
                  print(IterateArray($data_value));
              }
          } else {
              print($data);
              print("\n");
          }
      }
      
          // Call IterateArray()
      
      IterateArray($arr);
      
      ?>
      

      IterateArray()这里会打印不是数组的数据,否则会遍历数组的元素,并在这些元素上调用IterateArray()

      这个解决方案的好处是:如果你的数据结构发生变化,你的函数就不必改变了!

      【讨论】:

      • 哇!个人不知道这是否可行,但看起来很有希望。谢谢 HoldOffHunger!
      • 我不确定我是否理解正确,也不确定我是否能正确解释我的想法,但它在自身内部调用 IterateArray 函数,创建一个循环,该循环将采用一个新参数,即$data_value?
      • @TerranceBanh:是的!它是一个递归函数。你可以做更多的事情,比如通过递增缩进等等。但它适用于任何情况,这就是我喜欢它的原因。 (抱歉没早点回复,睡着了。)
      • 酷@HoldOffHunger。再次感谢!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 2020-09-06
      相关资源
      最近更新 更多