Friday, April 15, 2011

for each in php

1. For each bring one value at a time

<?php

 $fruit
= array( "Banana","Apple", "Plum");

foreach (
$fruit as $fruit_name)
{
    echo 
"$fruit_name<br/>\n";
}
?>
 


The above will display:
Banana
Apple
Plum 
2. For each bring two value in the array


<?php

$fruitColours 
= array("Banana" => "Yellow","Apple"  => "Green","Plum"   => "Purple");

foreach (
$fruitColours as $fruit_name => $fruit_colour)
{
    echo 
"$fruit_name is $fruit_colour<br/>\n";
}
?>




The above will display:
Banana is Yellow
Apple is Green
Plum is Purple

LinkWithin

Related Posts Plugin for WordPress, Blogger...