Search This Blog

for each statements with arrays in php

<?php


$birds= array('Parrots' =>
array('Kakapo','Kea ','Vernal Hanging Parrot'),

'Cuckoos and Turacos'=>
array('Purple-crested Turaco','Grey Go-away-bird','Great Blue Turaco'));
//display all
print_r($birds);


//for new line
echo '<br><br><br>';

// with foreach() display only main array
foreach($birds as $category => $sub){

echo '<b>'.$category.'<br></b>';
}

//for new line
echo '<br><br>';

// with foreach() display main array and sub arrays
foreach($birds as $category => $sub1){
echo '<b>'.$category.'<br></b>';
foreach($sub1 as $sub2)
echo $sub2.'<br>';
}
?>

output ;

Array ( [Parrots] => Array ( [0] => Kakapo [1] => Kea [2] => Vernal Hanging Parrot ) [Cuckoos and Turacos] => Array ( [0] => Purple-crested Turaco [1] => Grey Go-away-bird [2] => Great Blue Turaco ) )


Parrots
Cuckoos and Turacos


Parrots
Kakapo
Kea
Vernal Hanging Parrot
Cuckoos and Turacos
Purple-crested Turaco
Grey Go-away-bird
Great Blue Turaco

Share Links

Related Posts Plugin for WordPress, Blogger...