foreach statement traversal array
The foreach statement is used to iterate through an array, and each time a loop is made, the value of the current array element is assigned to the variable value (or other variable), and the array pointer moves one by another.
code example: foreach ($array as $value) {//$array the array to traverse, $value pointer to the current value of the array, as to the function of the assignment
code to executed;
The}foreach statement can also get the key name of the array, as follows:
code example: foreach ($array as $key = = $value) {
echo $key "-" $value. "<br/>";
}
Echo () function
Each () splits the key-value pair of the current element of the array into a new array and takes the next element as the current element. such as Array (..., ' Robert ' = ' Bob ',...) The ' robert ' = ' Bob ' key pair, split into array ([1]=> ' Bob ',[value]=> ' Bob ',[0]=> ' Robert ',[key]=> ' Robert ') arrays, Split into two sets (a total of four key-value pairs) return, 0 and 1 of the ordinal pair, key and value of the name-value pairs, using one of the set is OK.
Cases:
code example: <? Php
$prices =array (' Tries ' = +, ' oil ' =, ' Spank plugs ' = 4);
while ($elements = each ($prices)) {
echo $elements [' key ']; echo $elements [0];
echo "-";
echo $elements [' value ']; echo $element [1];
echo "<br/>";
}
Output Result:
Tires-100
Oil-10
Spank Plugs-4
The list () function iterates through the array
The list () function can be used to decompose an array into a series of values. The list () is often used with each (). However, List () can also not be used with each (), for example: List ($key, $value) = Explode () (":", $v);
List ($key, $value) = each ($array); $key, $value can name variable names arbitrarily
The sentence is assigned to $key, $value variable, of the element that is the current 0,1 position of the array returned by each.
Example:
code example: <?php
$prices =array (' Tries ' = +, ' oil ' =, ' Spank plugs ' = 4);
while (list ($product, $prices) = each ($prices);) {
echo $product "-" $prices;
echo "<br/>";
}
Output Result:
Tires-100
Oil-10
Spank Plugs-4
Another way to implement this:
code example: <?php
$prices =array (' Tries ' = +, ' oil ' =, ' Spank plugs ' = 4);
List ($product, $price) = $each ($prices);
echo "$product-$price"; Outputs the first array of
$next = $next ($prices); Pointer moves back
Echo $next;a method of traversing a two-dimensional array using the while, each, and list functions
<?PHP$contact=Array( ' Gao ' =Array(' ID ' =>1, ' name ' = ' high ', ' company ' = ' a ', ' addr ' = ' Beijing ', ' phonenumber ' = ' (010) 98765432 ', ' email ' = > ' [email protected] ', ' li ' =Array(' ID ' =>2, ' name ' = ' Lee ', ' company ' = ' B ', ' addr ' = ' Shanghai ', ' phonenumber ' + ' (021) 98765432 ', ' email ' = > ' [email protected] ', ' ma ' =Array(' ID ' =>3, ' name ' = ' ma ', ' company ' = ' C ', ' addr ' = ' Chongqing ', ' phonenumber ' = ' (023) 98765432 ', ' email ' = > ' [email protected] ', ' fan ' =Array(' ID ' =>4, ' name ' = ' fan ', ' company ' = ' d ', ' addr ' = ' Tianjin ', ' phonenumber ' = ' + ' (022) 98765432 ', ' email ' = > ' [email protected] ') ); while(List($key,$value)= each($contact)){ while(List($name,$info)= each($value)){ Echo $name.‘:‘.$info.‘ <br> '; } Echo"------------------------------------<br>"; } ?>
The effect of using the while, each, and list functions in PHP to traverse a two-dimensional array is:
List and mysql_fetch_row ():
Mysql_fetch_row (): gets a row from the result set as an enumeration array.
Data table field: Id,country,animal,cname
Echo ' <table cellpadding= "border=" 1 ">"; Echo ' <tr><th> field 1</th><th> field 2</th><th> field 3</th><th> field 4</th ></tr> '; while (list ($id, $country, $animal, $cname) =mysql_fetch_row ($result)) { echo "<tr>"; echo "<td>". $id. " </td> "; echo "<td>". $country. " </td> "; echo "<td>". $animal. " </td> "; echo "<td>". $cname. " </td> "; echo "</tr>"; } echo "</table>"; }
Reference: http://blog.csdn.net/maminyao/article/details/7569079
Http://www.169it.com/blog_article/1443995156.html
Loops in PHP iterate through the foreach list each