遍历多维关联数组PHP

时间:2018-09-25 23:39:25

标签: php multidimensional-array foreach 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循环以1对1的方式回显所有字符串,但似乎单词数组本身以某种方式引起了错误。我相信这是我最合乎逻辑的方法。

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

?>

不幸的是,我没有使用此递归函数。硬编码循环的每个实例,使我可以为每个嵌套循环执行不同的操作。尽管(如果我错了,请纠正我),如果多维数组的每个维都重复相同的代码,这将很有用。

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

欢迎使用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);

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

欢迎您!

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

有关此解决方案的妙处:如果您的数据结构发生变化,则不必更改功能!