Home  >  Article  >  Backend Development  >  What are the four ways to traverse an array in php

What are the four ways to traverse an array in php

青灯夜游
青灯夜游Original
2022-04-12 20:15:484977browse

Four methods: 1. Use "for($i=0;$i

What are the four ways to traverse an array in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Method 1: Use for statement traversal

The for loop will pre-define the variable that controls the number of loops in the for statement, so the for loop statement can perform loop operations according to the known number of loops, which is suitable for situations where the number of loops is clearly known. The number of times the script needs to be run.

The syntax format of the for loop is as follows:

for (初始化语句; 循环条件; 变量更新--自增或自减) {
    语句块;   
}

Statement analysis:

  • Initialization statement (expression 1): mainly to initialize a The variable value is used to set a counter, which is the value at the beginning of the loop; this statement is only executed during the first loop and will not be executed again in the future.

  • Loop condition (expression 2): Restriction condition for loop execution, used to control whether to execute the code in the loop body; if the condition is TRUE, the loop continues, if the condition is FALSE , the loop ends and exits the loop immediately.

  • Variable update (expression 3): an expression with an increment or decrement operation. Every time the loop is executed, the value of the counter is immediately modified so that the loop The conditions gradually become "untenable".

  • Statement block: Several codes that need to be executed when the condition is judged to be true.

Example

<?php
header("Content-type:text/html;charset=utf-8");
$array= array(2,"数学","梨子",3.14,"榴莲",12);
for ($i=0; $i < count($array); $i++) { 
    echo $array[$i] . "<br/>";
} 
?>

Output result:

2
数学
梨子
3.14
榴莲
12

Method 2: Use foreach statement to traverse

foreach is a statement specially designed for traversing arrays. It is a commonly used method when traversing arrays. It provides great convenience in traversing arrays. After PHP5, you can also traverse objects (foreach can only be applied to arrays and objects).

The foreach statement traverses the array regardless of the array subscript, and can be used for discontinuous index arrays and associative arrays with strings as subscripts.

foreach statement analysis

Let’s first understand the foreach statement, which has two grammatical formats:

Grammar format 1:

foreach ($array as $value){
    语句块;
}

Traverse the given $array array and assign the value of the current array to $value in each loop.

Syntax format 2:

foreach ($array as $key => $value){
    语句块;
}

Traverse the given $array array, and in each loop, the value of the current array will be assigned to $value, and the key name will be assigned to $key.

Explanation:

When the foreach statement loops, the pointer inside the array will move forward one step, so that the next array element will be obtained in the next loop. Stop traversing and exit the loop until it reaches the end of the array.

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
foreach ($array as $val){
    echo "值是:" . $val ;
    echo "<br/>";
}
echo "<br/>";
foreach ($array as $key => $value) { 
    echo "键名为:".$key.",键值为:".$value . "<br/>";
} 
?>

Output result:

What are the four ways to traverse an array in php

Method 3 : Use the while statement each() list() to traverse

each() function returns the current key value in the array and moves the array pointer forward. After each() is executed, the array pointer will stay at the next element in the array or at the last element when the end of the array is reached.

The list() function is used to assign values ​​to a set of variables in one operation.

while loop each() function list() function can traverse the array, let’s take a look at the code example below:

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
while( list($key, $val) = each($array) ) {
    echo "$key => $val<br>";
}
?>

Output result:

What are the four ways to traverse an array in php

Method 4: Use do-while statement next() key() current() to traverse

Every array in PHP has one Internal pointer, that is, array pointer; this pointer will point to an element in the array (this element is the current element of the array, and by default initially points to the first element in the array).

  • next() function can return the value of the next element pointed to by the internal pointer of the array, and returns FALSE when there are no more elements.

  • key() function is used to obtain the key name of the current element in the array.

  • current() function returns the key value of the current element of the array.

Use do-while loop next() key() current() to traverse the array and obtain the key name and key value of the array element.

Let’s take a look at the code example below:

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
do{
	 echo key($array)." => ".current($array)."<br>";
}while(next($array));
?>

Output result:

What are the four ways to traverse an array in php

After the traversal is completed, the array pointer will point to the last one For array elements, we can use the reset() function to reset the array pointer and redirect the internal pointer in the array to the first element.

Recommended: "PHP Video Tutorial"

The above is the detailed content of What are the four ways to traverse an array in php. For more information, please follow other related articles on the PHP Chinese website!

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