How to loop through one-dimensional array in php

PHPz
Release: 2023-04-23 09:59:44
Original
803 people have browsed it

In PHP, looping through one-dimensional arrays is a common task. A one-dimensional array is an array with only one dimension, that is, each element has only one value.

To loop through a one-dimensional array, you can use a for loop, foreach loop or while loop. Below we introduce the usage of these three types of loops respectively.

for loop

The for loop is one of the most basic methods when traversing a one-dimensional array. The following is an example:

$array = array("apple", "banana", "cherry");

for ($i = 0; $i < count($array); $i++) {
    echo $array[$i] . "<br>";
}
Copy after login

In this example, we use the count() method to get the length of the array, set the starting value and termination condition of the loop variable $i, and use echo in the loop body The statement outputs the array elements.

foreach loop

In addition to the for loop, PHP also provides the foreach loop, which can more conveniently traverse a one-dimensional array. Here is an example:

$array = array("apple", "banana", "cherry");

foreach ($array as $value) {
    echo $value . "<br>";
}
Copy after login

In this example, we directly use the foreach loop to traverse the array, store each element in the variable $value in turn, and output it.

In addition to the $value variable above, we can also use the $key variable to get the subscript of the array element. Here is an example:

$array = array("apple", "banana", "cherry");

foreach ($array as $key => $value) {
    echo $key . " : " . $value . "<br>";
}
Copy after login

In this example, we use the $key variable to output the index and value of each element.

while loop

The while loop can also be used to traverse a one-dimensional array, but you need to manage the loop variables and termination conditions yourself. The following is an example:

$array = array("apple", "banana", "cherry");

$i = 0;
while ($i < count($array)) {
    echo $array[$i] . "<br>";
    $i++;
}
Copy after login

In this example, we manually manage the loop variable $i, and use the count() method as the termination condition to continuously traverse the array and output each element.

Summary

The above are the three methods of looping through one-dimensional arrays in PHP: for loop, foreach loop and while loop. In actual development, we can choose which method to use according to the specific situation in order to complete the task more effectively and improve the readability and maintainability of the code.

The above is the detailed content of How to loop through one-dimensional array 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