PHP/Data Structure/Array Loop

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

List array values

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



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

 
<?php 
    $birds = array("a", "b", "c", "d"); 
    
    $limit = count($birds); 
    for($i = 0; $i < $limit; $i++) 
        printf("<p>(%d) %s.</p>\n", $i, ucfirst($birds[$i])); 
?>



Looping through an Enumerated Array

 <?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";
  }
?>



Looping Through a Sparse Array

 
<?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]); 
?>



Looping through child elements with foreach()

 
<?
$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";
}
?>



Looping through identically named elements with foreach()

 
<?
$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";
}
?>



Looping Through the $_SERVER Array

 
<html>
<head>
<title>Looping through the $_SERVER array</title>
</head>
<body>
<div>
<?php
foreach ( $_SERVER as $key=>$value ) {
   print "\$_SERVER[\"$key\"] == $value<br/>";
}
?>
</div>
</body>
</html>



Looping with foreach()

 
<?
$meal = array("breakfast" => "A",
              "lunch" => "B",
              "snack" => "C",
              "dinner" => "D");
print "<table>\n";
foreach ($meal as $key => $value) {
    print "<tr><td>$key</td><td>$value</td></tr>\n";
}
print "</table>";
?>



Using foreach to Iterate through an Array

<?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("<b>" . $language . "</b> <br />");
          }else{
               print($language . " <br />");
          }
     }
?>