PHP/Data Structure/Array Loop

Материал из Web эксперт
Версия от 10:02, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

List array values

   <source lang="html4strict">

<html>

<head>
 <title>List array values</title>
</head>
<body> 
    <?php $arr = array( "Red", "Green", "Blue" ); foreach( $arr as $value ) { echo("
  1. Do you like $value ?
  2. ");
     }
     ?>
    
</body>

</html>

 </source>
   
  


Looping Through a Compact Indexed Array Using for and count()

   <source lang="html4strict">

<?php

   $birds = array("a", "b", "c", "d"); 
   
   $limit = count($birds); 
   for($i = 0; $i < $limit; $i++) 
printf("

(%d) %s.

\n", $i, ucfirst($birds[$i]));

?>

 </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>
   
  


Looping Through a Sparse Array

   <source lang="html4strict">

<?php error_reporting(); $array = array("a" => "R", "b" => 2, c => "2"); foreach($array as $element)

   print("$element,"); 

$limit = count($array); for($i = 0; $i < $limit; $i++)

   printf("%s,", $array[$i]); 

?>

 </source>
   
  


Looping through child elements with foreach()

   <source lang="html4strict">

<? $menu=<<<_XML_ <?xml version="1.0" encoding="utf-8" ?> <rss version="0.91">

<channel>
 <title>Dinner</title>
 <link>http://example.ru/</link>
 <description>choices</description>
 <item>
  <title>Feet</title>
  <link>http://example.ru</link>
  <description>test</description>
 </item>
</channel>

</rss> _XML_; $xml = simplexml_load_string($menu); foreach ($xml->channel->item[0] as $element_name => $content) {

   print "The $element_name is $content\n";

} ?>

 </source>
   
  


Looping through identically named elements with foreach()

   <source lang="html4strict">

<? $menu=<<<_XML_ <?xml version="1.0" encoding="utf-8" ?> <rss version="0.91">

<channel>
 <title>What"s For Dinner</title>
 <link>http://example.ru/</link>
 <description>choices</description>
 <item>
  <title>Feet</title>
  <link>http://example.ru</link>
  <description>test</description>
 </item>
</channel>

</rss> _XML_; $xml = simplexml_load_string($menu); foreach ($xml->channel->item as $item) {

   print "Title: " . $item->title . "\n";

} ?>

 </source>
   
  


Looping Through the $_SERVER Array

   <source lang="html4strict">

<html> <head> <title>Looping through the $_SERVER array</title> </head> <body>

<?php foreach ( $_SERVER as $key=>$value ) {

  print "\$_SERVER[\"$key\"] == $value
";

} ?>

</body> </html>

 </source>
   
  


Looping with foreach()

   <source lang="html4strict">

<? $meal = array("breakfast" => "A",

             "lunch" => "B",
             "snack" => "C",
             "dinner" => "D");
print "\n"; foreach ($meal as $key => $value) { print "\n";

}

print "
$key$value
";

?>

 </source>
   
  


Using foreach to Iterate through an Array

   <source lang="html4strict">

<?php

    $prog_language[] = "PHP";
    $prog_language[] = "C";
    $prog_language[] = "C++";
    $prog_language[] = "Java";
    $prog_language[] = "Forth";
    $prog_language[] = "Perl";
    $prog_language[] = "Python";
    $prog_language[] = "Ruby";
  
    foreach($prog_language as $language)
    {
         if(!strcmp($language, "PHP")){
              print("" . $language . " 
"); }else{ print($language . "
"); } }

?>

      </source>