How to loop output array elements in php

WBOY
Release: 2023-05-19 22:38:37
Original
2284 people have browsed it

In PHP, array is a very common data structure. Looping through array elements is a common task that can be accomplished using different methods. This article will introduce how to loop through array elements in PHP.

1. Use for loop to output array elements

The for loop is the most basic loop structure in PHP. By iterating over the length of the array, we can use a for loop to output each element of the array.

Sample code:

<?php
 $arr = array('apple', 'banana', 'orange');
 for($i = 0; $i < count($arr); $i++) {
     echo $arr[$i] . "<br>";
 }
?>
Copy after login

We first define an array $arr, and then traverse and output each element through a for loop. Run the code and the output result is as follows:

apple
banana
orange
Copy after login
Copy after login
Copy after login
Copy after login

2. Use foreach loop to output array elements

The foreach loop is another loop structure in PHP. Usually, when processing arrays, it is more convenient and flexible to use a foreach loop.

Sample code:

<?php
 $arr = array('apple', 'banana', 'orange');
 foreach($arr as $item) {
     echo $item . "<br>";
 }
?>
Copy after login

In the above code, we use the foreach loop to traverse each element in the output array $arr. Run the code and the output result is as follows:

apple
banana
orange
Copy after login
Copy after login
Copy after login
Copy after login

3. Use while loop to output array elements

The while loop is a conditional loop structure. When the condition is true, the loop body is executed repeatedly until the condition is no longer true. You can use a while loop to output each element in the array.

Sample code:

<?php
 $arr = array('apple', 'banana', 'orange');
 $i = 0;
 while($i < count($arr)) {
     echo $arr[$i] . "<br>";
     $i++;
 }
?>
Copy after login

In the above code, we use a while loop to output each element in the array $arr. The number of loops is controlled through a count variable $i. Run the code and the output result is as follows:

apple
banana
orange
Copy after login
Copy after login
Copy after login
Copy after login

4. Use do-while loop to output array elements

The do-while loop is similar to the while loop, except that it will execute the loop body once first, and then Determine whether the condition is met. Therefore, using a do-while loop can also output each element in the array.

Sample code:

<?php
 $arr = array('apple', 'banana', 'orange');
 $i = 0;
 do {
     echo $arr[$i] . "<br>";
     $i++;
 } while ($i < count($arr));
?>
Copy after login

In the above code, we use a do-while loop to output each element in the array $arr. The number of loops is also controlled through a count variable $i. Run the code and the output is as follows:

apple
banana
orange
Copy after login
Copy after login
Copy after login
Copy after login

Summary:

In PHP, looping out array elements is a basic operating skill. Learning and mastering these loop structures is very important for developers . In the actual development process, different loop structures can be selected according to different needs. Whether you use a for loop, foreach loop, while loop or do-while loop, you can easily loop through the array elements.

The above is the detailed content of How to loop output array elements in php. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!