PHP 4 ways to iterate through an array _php tutorial

Source: Internet
Author: User
There are many functions in PHP that can be used to iterate over an array, such as: For statement, list, each, foreach, four functions, which is also a few of the main functions of traversing the array in PHP, let me introduce to you.

foreach Traversal array

When we use arrays, we often want to iterate over groups and get the values of keys or elements, and PHP provides a number of functions that specialize in iterating through arrays. This first introduces the use of the Foreach traversal array function.

Structural form:
Copy the Code code as follows:
foreach (array_expression as $value) statement
/* Array_expression is the array to traverse
As function is to assign the value of the array to the $value
Statement is a follow-up statement
*/

Example 1:
Copy the Code code as follows:
' White ',
' Black ' = ' dark ',
' Red ',
' Green ' and ' greens ',
' Yellow ' = ' yellow ');
The echo $c of the foreach ($color as $c).
";
?>

By using foreach, you can not only get the value of an element, but also get the key name, structure form:
Copy the Code code as follows: foreach (array_expression as $key = $value) statement
Add the code for line 7th in the above example:
Copy the code as follows: foreach ($color as $c) echo $c. "
";
Switch
Copy the code as follows: foreach ($color as $key + $c) echo $key. $c. "
";

Each traversal array

Traversing an array is an important part of the PHP array operation, in addition to the previously mentioned foreach function, which introduces a function-each () that iterates through the array.

Use the each () function to output the key name of the current pointer position and the corresponding element value. You can use "0″ or" key to access the key name (identifier) and the value corresponding to the identifier with "1″ or" value ".

Instance:
Copy the Code code as follows:
<?php
$languages =array (1=--> "PHP",
5=> "HTML",
10=> "CSS");
$a =each ($languages); /* First time to repeat the group */
echo $a [0]. " T ";
echo $a [1]. "
";
$a =each ($languages); /* Second time to the group */
echo $a [key]. " T ";
echo $a [value];
?>

List Traversal array

A function list can be assigned to a variable once while traversing an array, usually with each () function. Using the list () function makes it easier to access the keys and values returned by each ().

Instance:

Copy the Code code as follows:
<?php
$date =array (1=--> "Monday",
2=> "Tuesday",
3=> "Wednesday");
List ($key, $value) =each ($date); /* Traversal function */
echo "$key $value".
"; /* Output First array */
$next =next ($date); /* The pointer moves back */
echo "$next";
?>

The Ps:list () function is just the opposite of the array () function, array () creates an array of data, and list () splits the array into data.

For traversal arrays

In addition to some of the predefined traversal array functions in PHP, we can also use the Loop property of the for statement to iterate through the output. Here's an example:

Copy the Code code as follows:
<?php
$a []= "Jacky"; /* Define Array */
$a []= "Andy Lau";
$a []= "Dawn";
$a []= "Aaron Kwok";
$s =count ($a); /* Count the number of arrays */
for ($i =0; $i < $s; $i + +) {/* Iterate array */
echo $a [$i]. "
"; /* Display Array */
}
?>

http://www.bkjia.com/PHPjc/824614.html www.bkjia.com true http://www.bkjia.com/PHPjc/824614.html techarticle There are many functions in PHP that can be used to iterate over an array, such as: For statement, list, each, foreach four functions, which is also a few of the main functions of traversing the array in PHP, let me ...

  • 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.