We used to think about how to implement the PHP array loop output, this article introduces the use of PHP four commonly used circular statements to achieve the singular group or multi-dimensional array loop output method.
In PHP, we can use the following looping statements:
While
Loop through the code block as long as the specified condition is true
Do...while
Executes the code block first, and then repeats the loop when the specified condition is true
For
Loop execution code block specified number of times
Foreach
Loops a block of code based on each element in the array
First we use PHP's own function to iterate over the PHP array.
The Array_keys () and array_values () functions are readily available to get a list of all the keywords and corresponding values in the array.
| The code is as follows |
Copy Code |
|
' Bacon and eggs ', ' lunch ' = ' roast beef ', ' dinner ' = ' lasagna ');
/* Returns the array (' Breakfast ', ' lunch ', ' dinner ') with numeric indices */
$result = Array_keys ($menu); Print_r ($result); Print "";
/*
Returns the array (' bacon and eggs ', ' roast beef ', ' lasagna ') with numeric indices */
$result = Array_values ($menu); Print_r ($result);
?> |
Iterating through the PHP array with foreach
The PHP foreach () syntax structure is used to traverse an operation or an output array, and foreach () can only be used to iterate over an array or an object, resulting in an error when trying to use it for another data type or an uninitialized variable.
| The code is as follows |
Copy Code |
List = Array (' upid ' = ' 1 ', ' title ' = ' Company news ', ' list ' =>array (' id ' = ' = ' 3 ', ' title ' = ' = ' Company news title test! ') ', ' intime ' = > ' 2009-29-5 '));
foreach ($list as $v = $a)
{
echo $a [' upid ']. ' -'. $a [' title '];
foreach ($a [' list '] as $b) {
echo $b [' title '];
}
} |
Using array key values
| The code is as follows |
Copy Code |
$arr _age = Array ("Wang" =>18, "Li" =>20, "Zhang" =>25);
foreach ($arr _age as $key = + $age) {
Echo $key, ': ', $age, '
';
}
?>
|
Run the example output:
Wang:18
Li:20
Zhang:25
When foreach starts executing, the pointer inside the array automatically points to the first cell, which means that reset () is not called before the Foreach Loop.
for () looping through an array
If you are an array that operates on successive key values, you can also use the for () loop to iterate through the array:
| The code is as follows |
Copy Code |
$shuzu =array ("ni", "Wo", "ta", "php", "MySQL"); $count =count ($shuzu); echo "Using for traversal array"; echo " $nbsp; "; for ($i =0; $i < $count; $i + +) { $j = $i +1; echo "{$j} elements are: $shuzu [$i]"; echo " $nbsp; "; } ?>
25
|
You can also use the list () and each () combination to iterate through the PHP array, but the test discovery is less efficient than foreach ().
List function
The list () function assigns values to a set of variables using the elements in the array.
Note that, like array (), list () is actually a language structure, not a function.
Grammar
List (Var1,var2 ...) Parameter description
Var1 required. The first variable that needs to be assigned a value.
VAR2 is optional. There can be multiple variables.
Hints and Notes
Note: This function is used only for arrays of numeric indexes and assumes that the numeric index starts at 0
*/
| The code is as follows |
Copy Code |
|
$colors = Array (' Red ', ' blue ', ' green ', ' yellow ');
while (list ($key, $val) = each ($colors)) {
echo "Other list of $val.
";
} |
For more detailed information, please see: http://www.bKjia.c0m/phper/php/36112.htm
http://www.bkjia.com/PHPjc/629144.html www.bkjia.com true http://www.bkjia.com/PHPjc/629144.html techarticle we used to think about how to implement the PHP array loop output, this article introduces the use of PHP four commonly used circular statements to achieve the singular group or multi-dimensional array loop output method. In the ...