繁体   English   中英

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

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

我刚刚了解了键/值对。

我试图寻找现有的答案,并试图了解键/值对和关联数组的功能。 尽管这变得有点太耗时。

我在弄清楚如何在没有任何错误的情况下遍历此多维关联数组时遇到麻烦。

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

?>

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

foreach语句可以选择仅迭代所执行的值或键和值。 我怀疑这是您要寻找的:

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

欢迎来到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);
   $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!

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

IDEOne:IterateArray()的在线演示/ Sandox

<?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()

这个解决方案的妙处在于:如果您的数据结构发生变化,您的功能将无须改变!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM