简体   繁体   中英

Iterate through every single key and value in a multi-dimensional associative array 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')));

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.

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

Though it will result into the following:

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?

UPDATE: Thanks for the answers.

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

UPDATE:

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.

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

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

welcome to stackoverflow. Those things are Notices, you should not use echo with arrays, instead use print_r($ar) or var_dump($ar)

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

You can also print keys like this:

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

you are echoing array not string.

Welcome to SO!

I would like to post a solution that works for any-type of array, whether it is 3-d, 4-d, or any dimension. So, why not recursion , foreach() , and 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() here will print the data if it's not an array, and otherwise, it will iterate over the elements of the array, and call IterateArray() on these elements.

The nice part about this solution: if your data structure changes, your function won't have to!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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