PHP/Statement/foreach

Материал из Web эксперт
Перейти к: навигация, поиск

Array element order and foreach()

   <source lang="html4strict">

<? $letters[0] = "A"; $letters[1] = "B"; $letters[3] = "D"; $letters[2] = "C"; foreach ($letters as $letter) {

   print $letter;

} ?>

 </source>
   
  


Displaying the contents of an array using a loop

   <source lang="html4strict">

<?php $shapes = array("S" => "Cylinder",

               "N" => "Rectangle",
               "A" => "Sphere",
               "O" => "Sphere",
               "P" => "Rectangle");

foreach ($shapes as $key => $value) {

   print "The $key is a $value.
";

} ?>

 </source>
   
  


Foreach Demo

   <source lang="html4strict">

<html> <head> <title>Foreach Demo</title> </head> <body> <? $list = array("A", "B", "C", "D", "E");

print "
    \n"; foreach ($list as $value){ print "
  • $value
  • \n";

    } // end foreach

    print "
\n";

?> </body> </html>

      </source>
   
  


Implementing the traversable Iterator

   <source lang="html4strict">

<?php

   class IterateExample {
       public $var_one = 1;
       public $var_two = 2;
       private $var_three = 3;
       public $var_four = 4;
   }
   $inst = new IterateExample();
   foreach($inst as $key => $value) {
       echo "$key = $value\n";
   }

?>

 </source>
   
  


Iterating through a multidimensional array with foreach()

   <source lang="html4strict">

<? $flavors = array("Japanese" => array("hot" => "A",

                                    "salty" => "B"),
                "Chinese"  => array("hot" => "D",
                                    "pepper-salty" => "C"));

foreach ($flavors as $culture => $culture_flavors) {

   foreach ($culture_flavors as $flavor => $example) {
       print "A $culture $flavor flavor is $example.\n";
   }

} ?>

 </source>
   
  


Iterating Through Object Properties

   <source lang="html4strict">

<?

   class Person {
           public $FirstName = "B";
           public $MiddleName = "T";
           public $LastName = "M";
           private $Password = "asdfasdf";
           public $Age = 29;
           public $HomeTown = "Edinburgh";
           public $FavouriteColor = "Purple";
   }
   $bill = new Person( );
   foreach($bill as $var => $value) {
           echo "$var is $value\n";
   }

?>

 </source>
   
  


Looping through an Enumerated Array

   <source lang="html4strict">
<?php
 $emp_det []= "A";
 $emp_det []= "B";
 $emp_det []= "C";
 $emp_det []= "D";
 $emp_det []= "E";
 $emp_det []= "F";
 foreach ($emp_det as $temp) {
   echo "$temp", "\n";
 }

?>

      </source>
   
  


Modifying an array with foreach()

   <source lang="html4strict">

<? $meals = array("A" => 1,

              "B" => 4.95,
              "C" => 3.00,
              "D" => 6.50);

foreach ($meals as $dish => $price) {

   $meals[$dish] = $meals[$dish] * 2;

} foreach ($meals as $dish => $price) {

   printf("The new price of %s is \$%.2f.\n",$dish,$price);

} ?>

 </source>
   
  


Using foreach() to Iterate Through an Array

   <source lang="html4strict">

<?php

   $myarray = array("php", "is", "cool");
   foreach($myarray as $key => $val) {
     echo "The value of index $key is: $val
"; } foreach($myarray as $val) { echo "Value: $val
"; }

?>

 </source>
   
  


Using foreach() with numeric arrays

   <source lang="html4strict">

<? $dinner = array("A",

               "B",
               "C");

foreach ($dinner as $dish) {

   print "You can eat: $dish\n";

} ?>

 </source>