遍历多维关联数组PHP中的每个键和值 [英] Iterate through every single key and value in a multi-dimensional associative array PHP

查看:97
本文介绍了遍历多维关联数组PHP中的每个键和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚了解了键/值对.

I have just learned about key/value pairs.

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

I tried looking for an existing answer and tried to understand what I could about key/value pairs and associative arrays. Though this is becoming a little too time-consuming.

我很难弄清楚如何在这个多维关联数组中进行迭代而不会出现任何错误.

I am having troubles figuring out how to iterate through this multi-dimensional associative array without getting any errors.

$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可以很好地呈现整个数组,但是我想实现的目标是能够抓住所有可能的键和值.

So I know print_r does the job perfectly fine rendering the whole array but what I am trying to achieve is to be able to grab every key and value possible.

我尝试通过foreach循环以1对1的方式回显所有字符串,但是似乎单词数组本身以某种方式引起了错误.我相信这是我最合乎逻辑的方法.

I try to echo all the strings 1 by 1 through foreach loops but it seems that the word array itself causes an error somehow. I believe this is my most logical approach to it.

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

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

So I'd like to know if there is something that I am missing or is this not possible to achieve?

更新:谢谢您的回答.

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

This is the code I used based on the answers I got:

  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的建议.

I really like HoldOffHunger's suggestion.

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

?>

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

Unfortunately, I had no use for this recursive function. Hard coding each instance of loop lets me do different things for each nesting loop. Though (and correct me if I'm wrong) this will be useful if each dimension of a multi-dimensional array were to repeat the same code.

推荐答案

foreach语句可以选择仅遍历正在执行的值或键和值.我怀疑这是您要寻找的东西:

The foreach statement has the option of iterating over just the values, as you're doing, or the keys and values. I suspect this is what you're looking for:

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

The notices you're getting are because you're trying to use echo on an array, which is not possible.

这篇关于遍历多维关联数组PHP中的每个键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆