0

I have the following php array:

Array ( [0] => fdsa.txt 
        [1] => lubuntu-19.10-desktop-amd64.iso
        [2] => Solicitacao_de_Liberacao_a_Rede_WIFI.xlsx
        [teste\] => Array ( [0] => pasta1.txt [1] => pasta2.txt ) 
        [teste2\] => Array ( [0] => pasta3.txt [1] => pasta4.txt ) )

I want to use foreach to run through the array and echo everything

foreach($map as $arquivo){
    echo $arquivo."<br/>";  
}

But i want to show the name of the arrays instead of what is inside them like this:

fdsa.txt 
lubuntu-19.10-desktop-amd64.iso
Solicitacao_de_Liberacao_a_Rede_WIFI.xlsx
teste\
teste2\
Looper BR
  • 13
  • 1

1 Answers1

0

I would use the is_array to test if it's an array -- If it is .. Then display the key rather than the value .. IE

foreach($map as $key => $arquivo){
    if ( is_array($arquivo) ){
        echo "$key <br/>";
    }else{
        echo "$arquivo <br/>";
    }  
}

Side note -- There is no need to concatenate your echo statement if you are using a single variable and double quotes. Just makes for cleaner code.

Zak
  • 6,976
  • 2
  • 26
  • 48