What are the keywords for traversing arrays in php

青灯夜游
Release: 2023-03-17 18:40:02
Original
1675 people have browsed it

The keywords for traversing arrays are: 1. for, which can perform loop operations according to the known number of loops. It is suitable for situations where the number of times the script needs to be run is clearly known; 2. foreach and as, foreach is specially used for For statements designed to traverse an array, foreach needs to be used with the as keyword; 3. while, used with the each() and list() functions to traverse the array; 4. do, used with while, next(), key(), The use of current() can traverse the array.

What are the keywords for traversing arrays in php

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

The keywords for traversing arrays in php are: for, foreach, while, do; among them, do needs to be used together with while to form a "do-while" structure for loop function.

Let’s learn how to use these keywords to traverse the array.

The for statement traverses the array

The for loop will pre-define the variable that controls the number of loops in the for statement, so the for loop statement can follow Looping with a known number of loops is suitable for situations where you know exactly how many times the script needs to run.

The syntax format of the for loop is as follows:

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

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/>";
} 
?>
Copy after login

Output result:

2
数学
梨子
3.14
榴莲
12
Copy after login

foreach statement traverses the array (foreach as keyword)

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 only Can 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){
    语句块;
}
Copy after login

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){
    语句块;
}
Copy after login

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/>";
} 
?>
Copy after login

Output result:

What are the keywords for traversing arrays in php

while statement each() list() 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>";
}
?>
Copy after login

Output result:

What are the keywords for traversing arrays in php

do-while statement next() key() current() traversal

Every array in PHP has an internal pointer, that is Array pointer; this pointer will point to an element in the array (the 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 closer look at the code example:

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

Output result:

What are the keywords for traversing arrays in php

遍历结束后,数组指针会指向最后一个数组元素,我们可以使用reset()函数来重置数组指针,将数组中的内部指针重新指向第一个元素。

扩展知识:do-while 循环

do-while 循环和 while 循环非常相似,区别在于表达式的值是在每次循环结束时检查而不是开始时。和一般的 while 循环主要的区别是 do-while 的循环语句保证会执行一次(表达式的真值在每次循环结束后检查),然而在一般的 while 循环中就不一定了(表达式真值在循环开始时检查,如果一开始就为 false 则整个循环立即终止)。

do-while 循环只有一种语法:

<?php
$i = 0;
do {
   echo $i;
} while ($i > 0);
?>
Copy after login

以上循环将正好运行一次,因为经过第一次循环后,当检查表达式的真值时,其值为 false($i 不大于 0)而导致循环终止。

推荐:《PHP视频教程

The above is the detailed content of What are the keywords for traversing arrays in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template