Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 747
no_freedom Avatar asked Dec 31 '25 12:12

no_freedom


1 Answers

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;
     }
   }
}
like image 113
Niklas Avatar answered Jan 03 '26 02:01

Niklas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!