PHP Traversal Loop Array implementation method, PHP loop array
Simply use the foreach for list every while to iterate through the array, including a normal one-dimensional array and a two-dimensional array traversal method, with a detailed description of how each function is used.
- Array ('aa ' =>33,' BB ' =>22,' www.phpfensi.com ' =>11),Array (' DD ' =>44, ' ee ' =>55,
Use the for foreach to traverse one-dimensional data:
- $i = 0; $i <count ($foreach);
- ' foreach['. $i.']= '. $foreach [$i].'
Then look at using foreach to traverse:
- ' Foreach= '. $v.'
It's all about traversing one-dimensional data, and here we look at two-dimensional arrays:
- foreach ($key =
- foreach ($k =
- ' $arr ['. $key. '] ['. $k.']= '.
- "
Take a look at list each:
The each () function generates an array of the key names and key values of the elements pointed to by the current internal pointer to the array, and moves the internal pointer forward.
The returned array contains four elements: The key is named 0,1,key and value. Unit 0 and key contain the key name of the array cell, and 1 and value contain the data.
If the inner pointer crosses the array range, this function returns FALSE.
syntax:each (array) parameter description
Array required, which specifies the arrays to be used.
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.
Syntax:list (var1,var2 ...) Parameter description
Var1 required, the first variable that needs to be assigned a value.
VAR2 is optional and can have multiple variables.
Tips and Comments:
Note: This function is used only for arrays of numeric indexes and assumes that the numeric index starts at 0.
- Array (' red ',' Blue ',' green ',
- while (list ($key,$val) = each (
- "Other list of $val.
";
For statement:
You can use the FOR statement if you have determined how many times the code block repeats.
Grammar:
-
Note: The For statement has three parameters, the first parameter initializes the variable, the second parameter holds the condition, the third parameter contains the increment required to execute the loop, if more than one variable is included in the initialization or increment parameter, a comma is required, and the condition must be computed as True or FALSE.
The foreach statement is used to iterate through an array.
For Each loop, the value of the current array element is assigned to the value variable (the array pointer moves one at a time)-and so on.
http://www.bkjia.com/PHPjc/1051884.html www.bkjia.com true http://www.bkjia.com/PHPjc/1051884.html techarticle PHP Traversal Loop Array Implementation method, the PHP loop array simply using the foreach for list every while to iterate through the array, including the normal one-dimensional array and two-dimensional array traversal method, the following detailed ...