To iterate over an array in PHP, you can use a foreach
loop or array functions like array_map
or array_walk
.
In this example,
$numbers
with some values.foreach
loop to iterate over each element of the array and print the elements.<?php
$numbers = [1, 2, 3, 4, 5];
foreach ($numbers as $number) {
echo $number . "\n";
}
?>
1 2 3 4 5
In this example,
$names
with some strings.foreach
loop to iterate over each element of the array and print the elements.<?php
$names = ['Alice', 'Bob', 'Charlie'];
foreach ($names as $name) {
echo $name . "\n";
}
?>
Alice Bob Charlie
In this tutorial, we learned How to Iterate Over an Array in PHP language with well detailed examples.