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


 

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

This section demonstrates the use of foreach loop in the php

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

Ads