PHP looping through the three methods of array list (), each () and while Summary

Source: Internet
Author: User
This article mainly introduces three methods of PHP looping array list (), each (), and while. This article focuses on the mixed use of these three methods, for more information, see ① Each () function

The each () function needs to pass an array as the parameter, return the key/value pair of the current element in the array, and move the array pointer to the next element. The key-value pair is returned with an array of 4 elements associated with the index. the key names are 0, 1, key, and value, respectively. The values of the key 0 and the key are the same. they are the key names of the array elements. values 1 and 1 contain the values of the array elements. If the internal pointer crosses the end of the array, each () returns FALSE. The use of the each () function is as follows:

The code is as follows:


<? Php
$ Contact = array ("ID" => 1, "name" => "gao", "company" => "Company ", "address" => "Beijing ",);
$ Id = each ($ contact); // returns the key/value pair of the first element in the array $ contact. it is an array with four elements.
Print_r ($ id); // output Array $ id: Array ([1] => 1, [value] => 1, [0] => ID, [key] => ID)

$ Name = each ($ contact); // returns the key/value pair of the second element in the array $ contact. it is an array with four elements.
Print_r ($ name); // output Array ([1] => Gao, [value] => Gao, [0] => name, [key] => name)

$ Company = each ($ contact );
Print_r ($ company); // output Array ([1] => company A, [value] => company A, [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)
?>

② List () function

This is not a real function, but a PHP language structure. List () assigns values to a group of variables with one-step operations, that is, assigning values in the array to some variables. List () can only be used as an array of numeric indexes and assumes that the array index starts from 0. The syntax format is as follows:

The code is as follows:


List (mixed varname, mixed ...) = Array_expression


The list () statement is very different from other functions in terms of usage, rather than directly receiving an array as a parameter. Instead, the value of each element in the array is assigned to each parameter in the list () function through the "=" operator. The list () function converts each of its parameters to variables that can be directly used in the script. The usage is as follows:

The code is 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 and third values in the array.

List (, $ power) = $ info; // The variable value is the value of the third element in the array.
?>

After learning the usage of the list () function in the previous example, combine the each () function with the list () function. The code is as follows:

The code is as follows:


<? Php
$ Contact = array ("ID" => 1, "name" => "gao", "company" => "Company ", "address" => "Beijing ",);

List ($ key, $ value) = each ($ contact );
Echo "$ key => $ value"; // output variables $ key and $ value, separated by "=>"
?>

③ While loop traversal array

The use of the each () and list () statements described above makes it easy to understand that if the while loop is used to traverse the array. The syntax format is as follows:

The code is as follows:


While (list ($ key, $ value) = each (array_expression )){
Loop body
}


The format of this consortium traverses 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 parameter variable $ key of the list () function. Assign the value in the current array element to the second parameter variable $ value in the list () function, and move the pointer inside the array one step backward after the each () statement is executed, therefore, the next time the while () statement loops, the key/value pairs of the next element in the array will be obtained. When the each () statement at the end of the array returns FALSE, the while () statement stops the loop and ends the array traversal.

The code is as follows:


<? Php
$ Contact = array (
"ID" => 1,
"Name" => "gao ",
"Company" => "Company ",
"Address" => "Beijing ",
"Telephone" => "(010) 98765432 ",
"EMAIL" => "gao@brophp.com ",
);

// Output the information of each element in the array in the form of an HTML list
Echo'

One contact information :';

While (list ($ key, $ value) = each ($ contact )){
Echo"
$ Key: $ value
";
}

Echo'
';
?>

You can also use the same way to nest and traverse multi-dimensional arrays. Although the result of while traversal array is the same as that of freach statement, the two methods are different. After the while statement is used to traverse the array, the each () statement has pointed the internal pointer of the passed array parameter to the end of the array. When the while statement is used to traverse the same array again, the array pointer is already at the end of the array, and the each () statement returns FALSE directly. the while statement is not executed in a loop. The reset () function is called only before the while statement is executed, and the array pointer is re-specified to the first element. The foreach statement will automatically reset the pointer position of the array. when foreach starts to execute, the internal pointer of the array will automatically point to the first unit. This means that you do not need to call the reset () function before the foreach loop.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.