Home > Backend Development > PHP Problem > How to traverse php array

How to traverse php array

王林
Release: 2023-05-11 10:57:36
Original
877 people have browsed it

In PHP, an array is a common data structure that can store multiple values. In actual development, it is often necessary to traverse the array to operate on each element or key-value pair. This article will introduce the traversal method of PHP array.

  1. for loop traverses an array

The for loop is the most basic way to traverse an array. You can use a for loop to access each element in an array in order of array index.

Code example:

$fruits = array("apple", "banana", "orange");
$count = count($fruits);
for ($i = 0; $i < $count; $i++) {
  echo $fruits[$i] . "
";
}
Copy after login

In the above code, the length of the $fruits array is obtained through the count() function, and then a for loop is used to traverse each element in the array, using the $i variable. Represents the index value of the current element, and finally uses the echo statement to output the value of the current element.

  1. foreach loop traverses the array

Compared with the for loop, using the foreach loop can traverse the array more conveniently. The foreach loop assigns each element in the array to the specified variable and traverses the array in sequence.

Code example:

$fruits = array("apple", "banana", "orange");
foreach ($fruits as $fruit) {
  echo $fruit . "
";
}
Copy after login

In the above code, use the foreach statement to iterate through each element in the $fruits array and assign it to the variable $fruit. Finally, use the echo statement to output the value of the variable $fruit. It should be noted that when using a foreach loop to traverse an array, you do not need to specify the array length.

  1. foreach loop traverses the array and obtains key-value pairs

In some cases, you need to obtain the key and value of the array element, then you can use the foreach loop A special form: foreach($array as $key => $value).

Code example:

$fruits = array("apple" => 3, "banana" => 2, "orange" => 1);
foreach ($fruits as $key => $value) {
  echo $key . " has " . $value . " item(s)
";
}
Copy after login

In the above code, each element of the $fruits array is a key-value pair, where the key represents the name of the fruit and the value represents the quantity of the fruit. When using a foreach loop to traverse an array, you can assign the keys and values ​​of the array elements to the two variables $fruit and $count respectively, and output the corresponding information.

  1. while loop traverses the array

In addition to the for loop and foreach loop, you can also use the while loop to traverse the array. You need to use the function each() to get the current element of the array and move the pointer to the next element.

Code example:

$fruits = array("apple", "banana", "orange");
while (list($index, $fruit) = each($fruits)) {
  echo "Index: $index, Fruit: $fruit
";
}
Copy after login

The function each() returns the current element of the array and returns the key and value of the current element in the form of an array. Use the list() function to assign the returned keys and values ​​to the two variables $index and $fruit respectively, and output the index and element information. When using a while loop to traverse an array, you need to use the reset() function to restore the pointer to the beginning of the array after outputting each element.

Summary:

The above are several common ways of PHP array traversal. The for loop can be used to traverse the array in order of array index, while the foreach loop can be used to traverse the array more conveniently. The keys and values ​​of array elements can be obtained using a special form of foreach loop. If you need to use a while loop to traverse an array, you can use the each() function to get the array elements, and the reset() function to restore the pointer to the beginning. Different traversal methods are suitable for different scenarios and need to be selected according to actual needs.

The above is the detailed content of How to traverse php array. 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