Home Tutorial Php Phparray php array foreach/how to use foreach loop to iterate and print all the values in an array

 
 

Share on Google+Share on Google+
php array foreach/how to use foreach loop to iterate and print all the values in an array
Posted on: October 28, 2009 at 12:00 AM
Advertisement
This section demonstrates the use of foreach loop in the php

  • Foreach is enhanced version of the for loop.
  • It is used to iterate through the array elements.
  • It is used only with the array.


Example of PHP Array Foreach Loop

<?php
$ar1
=array("mango","apple","orange","grapes");
foreach
($ar1 as $a)
echo
" ".$a;
$ar1=array("m"=>"mango","a"=>"apple","o"=>"orange","g"=>"grapes");
echo
"<br>";
foreach
($ar1 as $key=>$val)
echo
" ".$key."-->".$val;
?>

Output
mango apple orange grapes
m-->mango a-->apple o-->orange g-->grapes

Advertisement

Related Tags for php array foreach/how to use foreach loop to iterate and print all the values in an array:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 28, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com