PHP traversal Array list foreach each method summary

Source: Internet
Author: User
Tags foreach date array copy functions key

There are a number of functions that can be used to traverse an array in PHP, such as the For statement, list, each, and foreach, which is also a few of the main functions that traverse an array in PHP, and I'll introduce you to the following four functions. foreach Traversal array

When we use arrays, we often have to enumerate the groups and get the values of each key or element, and PHP provides some functions that specifically traverse the array. Here we first introduce the use of foreach traversal array functions.

Structure form:

foreach (array_expression as $value) statement
/* Array_expression is the array to traverse
As action is to assign the value of an array to $value
Statement is a subsequent statement
*/
Example 1:

The code is as follows Copy Code

<?php
$color =array (' White ' => ',
' Black ' => ' dark ',
' Red ' => ' Reds ',
' Green ' => ' greens ',
' Yellow ' => ' yellow ');
foreach ($color as $c) echo $c. " <br> ";
?>


By foreach not only can you get the value of the element but also get the key name, structure form:

foreach (array_expression as $key => $value) statement
The code in the 7th line of the above instance:

The code is as follows Copy Code


foreach ($color as $c) echo $c. " <br> ";

To

foreach ($color as $key => $c) echo $key. $c. " <br> ";


Each traversal array


Traversing an array is an important part of the PHP array operation, and in addition to the Foreach function mentioned earlier, here is a function-each () that traverses the array.

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

Instance:

The code is as follows Copy Code

<?php
$languages =array (1=> "PHP"),
5=> "HTML",
10=> "CSS");
$a =each ($languages); /* The first time in the enumeration group * *
echo $a [0]. " T ";
echo $a [1]. " <br> ";
$a =each ($languages); * * The second time to enumerate the group * *
echo $a [key]. T ";
echo $a [value];
?>

List Traversal array

The function list can be assigned to a variable at one time while traversing an array, and is usually used in conjunction with each () function. The list () function makes accessing the keys and values returned by each () easier.

Instance:

The code is as follows Copy Code
<?php
$date =array (1=> "Monday"),
2=> "Tuesday",
3=> "Wednesday");
List ($key, $value) =each ($date); /* Traversal function * *
echo "$key $value." <br> "; /* Output First Array * *
$next =next ($date); /* Pointer to move 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 array

In addition to some of the predefined traversal array functions in PHP, we can also use the loop attribute of the FOR statement to traverse the array to the output. Here's an example:

The code is as follows Copy Code

<?php
$a []= "Jacky Cheung"; /* Define Array/*
$a []= "Andy Lau";
$a []= "Dawn";
$a []= "Aaron Kwok";
$s =count ($a); /* Statistical array number * *
for ($i =0; $i < $s; $i + +) {/* Traverse array */
echo $a [$i]. " <br> "; /* Display Array/*
}
?>



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.