php nested array foreach or similar to iterate through a bidimensional array

StackOverflow https://stackoverflow.com/questions/14513718

  •  17-01-2022
  •  | 
  •  

Question

i´d like how to iterate through this array to print something like this in an html list ( a foreach or any other loop should do the work...). I´m completely stuck..!

Caballos  Soledad'
    'Paseo a caballo' 
...........
...........
...........
Rancho Valle
     'Camping'
    'Paseo a caballo' 

array (size=10)
  40 => 
    array (size=2)
      'proveedor' => string 'Caballos Soledad' (length=26)
      'servicio' => 
        array (size=1)
          0 => string 'Paseo a caballo' (length=15)
  41 => 
    array (size=2)
      'proveedor' => string 'Caballos Segundo' (length=25)
      'servicio' => 
        array (size=1)
          0 => string 'Paseo a caballo' (length=15)
  64 => 
    array (size=2)
      'proveedor' => string 'Cerro' (length=10)
      'servicio' => 
        array (size=1)
          0 => string 'Camping' (length=7)
  39 => 
    array (size=2)
      'proveedor' => string 'Caballos Nieto' (length=19)
      'servicio' => 
        array (size=1)
          0 => string 'Paseo a caballo' (length=15)
  174 => 
    array (size=2)
      'proveedor' => string 'Hacienda' (length=21)
      'servicio' => 
        array (size=2)
          0 => string 'Paseo a caballo' (length=15)
          1 => string 'Camping' (length=7)
  49 => 
    array (size=2)
      'proveedor' => string 'Campo ' (length=10)
      'servicio' => 
        array (size=1)
          0 => string 'Camping' (length=7)
  241 => 
    array (size=2)
      'proveedor' => string 'Lodge' (length=12)
      'servicio' => 
        array (size=1)
          0 => string 'Paseo a caballo' (length=15)
  258 => 
    array (size=2)
      'proveedor' => string 'Modesto' (length=14)
      'servicio' => 
        array (size=1)
          0 => string 'Paseo a caballo' (length=15)
  294 => 
    array (size=2)
      'proveedor' => string 'Rancho Valle ' (length=18)
      'servicio' => 
        array (size=2)
          0 => string 'Camping' (length=7)
          1 => string 'Paseo a caballo' (length=15)
  38 => 
    array (size=2)
      'proveedor' => string 'Caballos Jose ' (length=22)
      'servicio' => 
        array (size=1)
          0 => string 'Paseo a caballo' (length=15)

I'b been searching for too long... but not find a proper way to do that. Many thanks in advance

Was it helpful?

Solution

function my_print_r($array){
    echo '<ul>';
    foreach ($array as $key => $value){
        echo '<li>' . $key;
        if (is_array($value)){
            my_print_r($value);
        }
        else echo ' : ' . $value;
        echo '</li>';
    }
    echo '</ul>';
}

Hope this will work, just change the formatting as You like :)

OTHER TIPS

So, i changed the format as i like

function my_print_r($array){

    foreach ($array as $key => $value){

        if (is_array($value)){
            echo '<ul>';
            my_print_r($value);
            echo '</ul>';
        }
        else echo '<li> ' . $value;

    }
      echo '</li>';
   // echo '</ul>';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top