Share on Google+Share on Google+

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

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

Advertisements

Posted on: October 28, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.