phparraysloopsmultidimensional-array

How to traverse the variable depth items of a multidimensional array


I'm trying to print array. All code working fine, but at last I'm getting `ArrayArray'.

Here is my array

Array
(
[Post1] => Array
    (
        [id] => 1
        [title] => hi
    )
[Post2] => Array
    (
        [0] => Array
            (
                [id] => 1
            )
    )
[Post3] => Array
    (
        [0] => Array
            (
                [id] => 1
            )
    )
 )

Here is my PHP Code

foreach($post as $key => $value) {
 foreach($value as $print => $key) {
     echo "<br>".$key;
   }
}

here is output

ID
Array
Array

Solution

  • Try this:

    foreach($post as $key => $value) {
     foreach($value as $print => $key) {
         if (is_array($key)){
             foreach($key as $print2 => $key2) {
              echo "<br>".$key2;
              }
    
         }else{
         echo "<br>".$key;
         }
       }
    }