3 ways to iterate through an array list (), each (), and while and a detailed example

Source: Internet
Author: User
This article mainly introduces the 3 methods of the PHP loop traversal array list (), each () and while summary, focusing on the mixed use of the 3 methods of the explanation.

①each () function

each () function needs to pass an array as an argument, return the key/value pair of the current element in the array, and move the array pointer backwards to the next element's position. The key/value pairs are returned with an array of 4-element associative and indexed blends, with key names of 0, 1, key, and value, respectively. Where the key name 0 and key corresponding value is the same, is the key name of the array element, 1 and value contains the value of the array element. If the internal pointer crosses the end of the array, each () returns false. The use of the each () function is as follows:

<?php$contact = Array ("ID" = 1, "name" = "High", "Company" = "a Company", "Address" = "Beijing",), $id = each ($contact); Returns the key/value pair of the first element in the array $contact, which is an array with 4 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, which is an array with 4 elements print_r ($name); Output Array ([1]=> high,[value]=> a,[0]=> name,[key]=> name) $company = each ($contact);p rint_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)?>

②list () function

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

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

The list () statement differs greatly from other functions in its use, and does not receive an array directly as a parameter. Instead, the value of each element in the array is assigned to each parameter in the list () function by assigning a value to the "=" operator. The list () function also converts each parameter in it to a variable that can be used directly in the script. Use the following methods:

<?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 value of a variable is the value of the third element in the array?>

Using the example above to understand the use of the list () function, combine each () function with the list () function. The code looks like this:

<?php$contact = Array ("ID" = 1, "name" = "High", "Company" = "a Company", "Address" = "Beijing",); List ($key, $value) = each ($contact); echo "$key = = $value"; Output variables $key and $value, with "=" split in the middle?>

③while iterating through an array

The use of each () and list () statements described earlier is not difficult to understand if you are iterating through an array using a while loop. The syntax format used is as follows:

while (list ($key, $value) = each (array_expression)) {Loop Body}

The format of this union iterates over the given array of array_expression. In each loop of the while () statement, each () statement assigns the key of the current array element to the first argument variable $key of the list () function. and assigns the value in the current array element to the second parameter variable $value in the list () function, and each () statement then moves the pointer inside the array one step back, so that the next time () statement loops, it gets the key/value pair for the next element in the array. Until the end of the array each () statement returns the False,while () statement to stop the loop, ending 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",); Output the information for each element in the array as an HTML list echo ' <dl> a contact information: '; while (list ($key, $value) = each ($contact)) {echo "<dd> $key: $value </dd>";} Echo ' </dl> ';? >

You can also iterate through a multidimensional array in the same way. While the result of the while traversal array is the same as the Freach statement, there is a difference between the two methods. After iterating through the array using the while statement, the each () statement has pointed to the array end of the incoming array parameter internal pointer. When you use the while statement to traverse the same array again, the array pointer is already at the end of the array, and each () statement directly returns the False,while statement without being executed for the loop. Only the Reset () function is called before the while statement executes, and the array pointer is re-assigned to the first element. The foreach statement automatically resets the pointer position of the array, and when foreach starts executing, the pointer inside the array automatically points to the first cell. 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.