Three Ways of PHP Loop through Arrays list of each of and while Summary

  • 2021-08-05 09:12:56
  • OfStack

① each () function

The each () function passes an array as an argument, returns the key/value pair of the current element in the array, and moves the array pointer backward to the next element. The key/value pair is returned with a 4-element mixed array of associations and indexes with key names 0, 1, key, and value. The key name 0 and key correspond to the value of 1, which is the key name of the array element, and 1 and value contain the value of the array element. If the inner pointer crosses the end of the array, each () returns FALSE. The each () function is used as follows:


<?php
$contact = array("ID" => 1," Name " => " Gao Mou "," Company " => "A Company "," Address " => " Beijing ",);
$id = each($contact); // Return Array $contact Middle grade 1 Key of 1 element / Value pair, which is a pair with 4 An array of three elements 
print_r($id); // Output array $id : Array([1]=>1,[value]=>1,[0]=>ID,[key]=>ID)
 
$name = each($contact); // Return Array $contact The first of the 2 Key of 1 element / Value pair, which is a pair with 4 An array of three elements 
print_r($name); // Output Array([1]=> Gao Mou ,[value]=> Gao Mou ,[0]=> Name ,[key]=> Name )
 
$company = each($contact);
print_r($company); // Output Array([1]=>A Company ,[value]=>A Company ,[0]=> Company ,[key]=> Company )
 
$address = each($contact); 
print_r($address); // Output Array([1]=> Beijing ,[value]=> Beijing ,[0]=> Address ,[key]=> Address )
 
$no = each($contact);
var_dump($no); // Output bool(false)
?>

each () in PHP; All array values can be enumerated, and list () reads data from the subscript of element 0 in each (), such as:


<?php
$url=array(' Baidu =>'www.baidu.com',
         ' Sina. com '=>'www.sina.com',
         ' Sohu '=>'www.sohu.com' 
);
while(list($a,$b)=each($url))
echo "$a=$b <br/>";
?>

Explanation: The each () function is used to return the array value of the current pointer position and push the pointer one position. The return array contains four keys, with keys 0 and key containing the key names and keys 1 and value containing the corresponding data. If the pointer is already at the end of the array when the program executes the each () function, false is returned.

② list () function

The list () function assigns values to a set of variables with elements from an array.

Note that, like array (), list () is actually a language structure, not a function.

Grammar
list(var1,var2...)

参数 描述
var1 必需。第1个需要赋值的变量。
var2 可选。可以有多个变量。

Tips and Notes
Note: This function is only used for numerically indexed arrays, and assumes that numerically indexed arrays start from 0.


<?php
$my_array = array("Dog","Cat","Horse");

list($a, $b, $c) = $my_array;
echo "I have several animals, a $a, a $b and a $c.";
?>

It can be seen from the above example that list () can't directly read the data passed by form form, because the form data is not a digital index, but a variable name index, such as pwd= "123", pwd is an index, and 123 is a value, so it is necessary to establish a digital index with each () before assigning a value to list () function. While array () is a standard array, this will not be introduced.

This is not really a function, but the language structure of PHP. list () assigns values to a set of variables in a one-step operation, that is, assigns values from an array to a number of variables. list () can only be used with numerically indexed arrays and assumes that the array index starts at 0. The syntax format is as follows:

list (mixed varname, mixed …) = array_expression

The list () statement is very different from other functions in use, and does not take an array directly as an argument. Instead, the value of every element in the array is assigned to every parameter in the list () function by the "=" operator. The list () function, in turn, converts each argument in it into a variable that can be used directly in the script. Use as follows:


<?php
$info = array('coffee','brown','caffeine');
list($drink,$color,$power) = $info;
 
list($drink,,$power) = $info; // The value of the variable is the first in the array 1 Number and number 3 Values 
 
list( , ,$power) = $info; // The variable value is the first in the array 3 The value of 10 elements 
?>

After understanding the usage of the list () function through the above example, combine the each () function with the list () function. The code looks like this:


<?php
$contact = array("ID" => 1," Name " => " Gao Mou "," Company " => "A Company "," Address " => " Beijing ",);
 
list($key,$value) = each($contact);
echo "$key => $value"; // Output variable $key And $value , used in the middle " => "Segmentation 
?>

③ while loops through arrays

With the use of the each () and list () statements described earlier, it is not difficult to understand if an while loop is used to iterate through an array. The syntax format used is as follows:


while(list($key,$value) = each(array_expression)){
 Circulatory body 
}

The format of this union iterates over the given array_expression array. In each loop of the while () statement, the each () statement assigns the key of the current array element to the first argument variable $key of the list () function. The value in the current array element is assigned to the second parameter variable $value in the list () function, and the each () statement will move the pointer inside the array one step backward after execution, so the next time the while () statement loops, the key/value pair of the next element in the array will be obtained. Until the end of the array, the each () statement returns FALSE, and the while () statement stops the loop and ends the traversal of the array.


<?php
$contact = array(
"ID" => 1,
" Name " => " Gao Mou ",
" Company " => "A Company ",
" Address " => " Beijing ",
" Telephone " => "(010)98765432",
"EMAIL" => "gao@brophp.com",
);
 
// With HTML Output the information of each element in the array in the form of a list 
echo '<dl>1 Contact information: ';
 
while(list($key,$value) = each($contact)){
echo "<dd>$key : $value</dd>";
}
 
echo '</dl>';
?>

It can also be yo ES 113EN nested traversal of multidimensional arrays in the same way. Although the result of while traversing an array is the same as the freach statement, there is a difference between the two methods. After traversing the array using the while statement, the each () statement already points the internal pointer of the array parameter passed in to the end of the array. When the while statement is used again to traverse the same array, the array pointer is already at the end of the array, the each () statement returns FALSE directly, and the while statement is not looped. The reset () function is called only before the while statement executes to re-assign the array pointer to the first element. The foreach statement automatically resets the pointer position of the array, and when foreach starts execution, the pointer inside the array automatically points to the first cell. This means that the reset () function does not need to be called before the foreach loop.


Related articles: