Home  >  Article  >  Backend Development  >  php array traversal

php array traversal

巴扎黑
巴扎黑Original
2016-11-24 09:16:411231browse

/* Array traversal
*
* 1. Use the for statement to loop through the array
* a. Other languages ​​(only this way)
* b. This method in PHP is not our preferred method
*C. The array must be the index array, and the bidding must be continuous
*(the index array is bidding, it can also be discomfort, there is a control array in the array)
*
*
2. Use the FOREACH statement to circulate through the array of arrays through the array of arrays
* foreach (array variable as variable value) {
* //Loop body
* }
* a. The number of loops is determined by the number of elements in the array
* b. Each loop will assign the elements in the array to the following. Variable
*
* foreach (array variable as subscript variable => value variable) {
*
* }
*
* 3. while() list() each() combination loop traverses the array
*
* each( ) function,
*   a. Requires an array as a parameter
*     b. The returned array is also an array
*       c. The returned array is 0, 1, key, value four subscripts (fixed)
*     0 and The key subscript is the key of the current parameter array element
*   1 and the value subscript is the value of the current logarithmic array element
*
* d. By default, the current element is the first element
*       e. After each execution, it will Move the current element backward
*                                                                                                                                                                                ; id [key] => id )
*
* list() function

* a. list()=array(); You need to assign an array to this function
* b. The number of elements in the array, The number of parameters in the list() function must be the same
* c. Each element value in the array will be assigned to each parameter in the list() function, and list() will convert each parameter into a variable
* d. list () can only receive index arrays
* e. In the order of index subscripts
*/
/*
$user=array("id"=>1, "name"=>"zhangsan", "age" =>10, "sex"=>"nan");

while($arr=each($user)){
//echo $arr[0]."==>".$arr[ 1]."
";
echo $arr["key"]."---->".$arr["value"]."
";
}

Use array Internal pointer control function
next(array);
prev(array);
reset(array)
end(array);

current(array);
key(array);
*/

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn