PHP/Data Structure/Array Loop — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 10:02, 26 мая 2010
Содержание
- 1 List array values
- 2 Looping Through a Compact Indexed Array Using for and count()
- 3 Looping through an Enumerated Array
- 4 Looping Through a Sparse Array
- 5 Looping through child elements with foreach()
- 6 Looping through identically named elements with foreach()
- 7 Looping Through the $_SERVER Array
- 8 Looping with foreach()
- 9 Using foreach to Iterate through an Array
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("
- Do you like $value ? ");
} ?>
</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 "
$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>