0

Good Morning,

I have this piece of code:

$contenitoreArray = ["Ottimo Lavoro", "Ora", "Tu Puoi",["Inviare",["La tua","Casa"]]];
$fraseFinale = "";
function ricorsione($contenitoreArray) {
   foreach($contenitoreArray as $valoreArray){
    if(is_array($valoreArray)){
        ricorsione($valoreArray);
    }else{
        $fraseFinale .= $valoreArray;
    }
   }
    return $fraseFinale;
}

$peppe = ricorsione($contenitoreArray); 
echo $peppe;

My goal is to be able to print the entire array on the screen

thank you very much for your availability

Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
CavaForCoding
  • 21
  • 2
  • 10

1 Answers1

0

Here how you can simplify the code:

$contenitoreArray = ["Ottimo Lavoro", "Ora", "Tu Puoi",["Inviare",["La tua","Casa"]]];
$fraseFinale = [];

// flatten the array to one dimension
array_walk_recursive($contenitoreArray, function ($v) use (&$fraseFinale) { $fraseFinale[] = $v; });
// next - just implode with the required glue parameter
echo implode(', ', $fraseFinale);

More ways to flatten the array are described here.

Simone Rossaini
  • 8,115
  • 1
  • 13
  • 34
u_mulder
  • 54,101
  • 5
  • 48
  • 64