Home  >  Article  >  Backend Development  >  Summary of 4 ways to traverse arrays in PHP_PHP Tutorial

Summary of 4 ways to traverse arrays in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:26:38794browse

There are many functions that can be used to traverse arrays in PHP. For example, there are four functions: for statement, list, each, and foreach. These are also the main functions for traversing arrays in PHP. Let me give them below. Introduce everyone.

foreach traverses the array

When we use arrays, we often need to traverse the array and obtain each key or element value. PHP provides some functions specifically for traversing arrays. Here we first introduce the usage of foreach array traversal function.

Structural form:

Copy code The code is as follows:

foreach (array_expression as $value) statement
/*array_expression is the array to be traversed
The function of as is to assign the value of the array to $value
Statement is the subsequent statement
*/

Example 1:

Copy code The code is as follows:

'White' ,
'black' => 'black' ,
'red' => 'red' ,
'green' => 'green',
'yellow' => 'yellow');
foreach( $color as $c) echo $c ."
"; 
?>

Not only the value of the element but also the key name can be obtained through foreach. The structural form:

Copy code The code is as follows:
foreach ( array_expression as $key => $value ) statement

Change the code on line 7 in the above example:
Copy code The code is as follows:
foreach( $color as $c) echo $c ."
";

Change to:
Copy code The code is as follows:
foreach( $color as $key => $c) echo $key.$c ."

each traverses the array

Traversing an array is an important part of PHP array operations. In addition to the foreach function mentioned above, here is another function for traversing an array -each().

Use 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 value corresponding to the identifier.

Example:

Copy code The code is as follows:

$languages=array(1=-->"php",
5=>"html",
10=>"css");
$a=each($languages); /* First traversal of the array */
echo $a[0] ."t";
echo $a[1] ."
";
$a=each($languages); /* Traverse the array for the second time */
echo $a[key] ."t";
echo $a[value];
?>

list traverses array

The function list can be assigned to variables once when traversing the array, and is usually used in conjunction with the each() function. Using the list() function makes it easier to access the keys and values ​​returned by each().

Example:

Copy code The code is as follows:

$date=array(1=-->"Monday",
2=>"Tuesday",
3=>"Wednesday");
list($key,$value)=each($date); /* Traversal function */
echo "$key $value" ."
"; /* Output the first array */
$next=next($date); /* Move the pointer backward */
echo "$next";
?>

ps: The list() function is just the opposite of the array() function. array() constructs a series of data into an array, while list() splits the array into data.

for traverses the array

In addition to some of the predefined array traversal functions in PHP, we can also use the loop feature of the for statement to traverse the array and output it. Examples are given below:

Copy code The code is as follows:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824614.htmlTechArticleThere are many functions that can be used to traverse arrays in php, such as: for statement, list, each, foreach These four functions are also the main functions for traversing arrays in php. Let me do it next...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn