Home Backend Development PHP Tutorial Several ways to loop through arrays in PHP using statements and functions

Several ways to loop through arrays in PHP using statements and functions

Jun 22, 2017 pm 03:22 PM
php use function cycle statement Traverse

Traverse an array in PHPThere are three commonly used methods:

1. Use the for statement to loop through the array;
2. Use the foreach statement to traverse the array;
3. Use list(), each() and while loop in combination to traverse the array.
The most efficient of these three methods is to use the foreach statement to traverse the array. The foreach structure has been introduced since PHP4. It is a statement specially designed for traversing arrays in PHP. It is recommended that everyone use it. Let’s first introduce these methods respectively.

1. Use the for statement to loop through an array
It is worth noting that using the for statement to loop through an array requires that the array traversed must be an index array. There are not only associative arrays but also index arrays in PHP, so the for statement is rarely used in PHP to loop through arrays.
The example code is as follows:

<?php 
$arr = array('http://www.php.cn','php中文网','PHP教程'); 
$num = count($arr); 
for($i=0;$i<$num;++$i){ 
echo $arr[$i].'<br />'; 
} 
?>
Copy after login

Note: In the above example code, we first calculate the number of elements in the array $arr, and then use it in the for statement. This is very efficient. Because if it is for($i=0;$i< count($arr);++$i), each loop will calculate the number of elements in the array $arr, and the above method can be used to subtract this overhead. The use of ++$i is also to improve efficiency. We have mentioned it in previous articles and I suggest you read it again.
The output result of the above code is:
http://www.php.cn
php中文网
PHP tutorial


2. Use the foreach statement Traversing an array
There are two ways to use the foreach statement to loop through an array. The first method is the one we use most. The introduction is as follows:
The first way:
foreach(array_expression as $value){
//Loop body
}
Example code:

&lt;?php 
$arr = array(&#39;http://www.php.cn&#39;,&#39;php中文网&#39;,&#39;PHP教程&#39;); 
foreach($arr as $value){ 
echo $value.&#39;&lt;br /&gt;&#39;; 
} 
?&gt;
Copy after login

In each loop , the value of the current element is assigned to the variable $value, and the pointer inside the array is moved backward one step. Therefore, the next element of the array will be obtained in the next loop, and the loop will not stop until the end of the array, ending array traversal.
Second way:
foreach(array_expression as $key=>$value){
//Loop body
}
Example code:

&lt;?php 
//定义数组 
$arr = array(&#39;http://www.php.cn&#39;,&#39;php中文网&#39;,&#39;PHP教程&#39;); 
foreach($arr as $k=&gt;$v){ 
echo $k.&quot;=&gt;&quot;.$v.&quot;&lt;br /&gt;&quot;; 
} 
?&gt;
Copy after login


##3. Combined use of list(), each() and while loop to traverse the array each() function needs to be passed Takes an array as an argument, returns the key/value pair of the current element in the array, and moves the array pointer backward to the position of the next element.
list() function, this is not a real function, it is a language structure of PHP. list() assigns values ​​to a set of variables in one step.
Example code:

&lt;?php 
//定义循环的数组 
$arr = array(&#39;website&#39;=&gt;&#39;http://www.php.cn&#39;,&#39;webname&#39;=&gt;&#39;php中文网&#39;) 
while(list($k,$v) = each($arr)){ 
echo $k.&#39;=&gt;&#39;.$v.&#39;&lt;br /&gt;&#39;; 
} 
?&gt;
Copy after login
The output result is:

website=>http://www.php.cn
webname=>php Chinese website
Summary: the above three Among the methods of looping through an array, it is recommended that you use the foreach statement to loop through the array, which is more efficient.

The above is the detailed content of Several ways to loop through arrays in PHP using statements and functions. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles