PHP: foreach 用法

2013052821:20
 

#1

<?php

$ARRAY=array(1,3,5,7,9);

foreach ($ARRAY as $value) {
   echo "Value: $value \n";
}

?>

 
 

#2

<?php

$ARRAY=array(
  "tiger" => 1,
  "dog" =>2,
  "cat" =>3'
);

foreach($ARRAY as $key => $value) {
  echo "$key ==> $value \n";
}

?>


 
 

#3 二維陣列

<?php 

$ARRAY = array(
0 => array (0 =>"hello",  1=>"book"),
1 => array (0 =>"dog",    1=>"cat"),
);

foreach($ARRAY as $val1) {
   foreach($val1 as $val2) {
       echo "$val2 ";
   }

}

?>



參考資料: