遍历多维关联数组中的每个键和值 PHP

Iterate through every single key and value in a multi-dimensional associative array PHP

我刚刚了解了 key/value 对。

我试图寻找现有的答案,并试图了解我对 key/value 对和关联数组的了解。虽然这变得有点太费时了。

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

$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);

?>

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

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,这是不可能的。

欢迎使用 Whosebug。 这些是注意事项,你不应该对数组使用 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);
   $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;    
            }
        }
    }

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

欢迎来到 SO!

我想要 post 一个适用于任何类型数组的解决方案,无论是 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()

此解决方案的优点在于:如果您的数据结构发生变化,您的函数将不必如此!