Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!
We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!
Learn from Guru Rajesh Kumar and double your salary in just one year.

In the query, we will discuss about” how can you create foreach loop with a numeric array in Php?”. Here, we discuss the foreach loop with a numeric array or you may say index array also. what about the foreach loop? you can use foreach loop merely on an array. For pairing through key values, we apply of foreach loop.
What is foreach in PHP?
The foreach() method is used in PHP which works on arrays and is used for creating a loop through each key/value pair in an array. it allows a loop through an element in indexed & associated arrays and also run a block of codes for each element.
Syntax of foreach() loop:
The syntax for indexed arrays is as given in the below-mentioned block:
foreach($array_name as $value){
block of statement;
}
Here, “Name” is the required parameter. It is the array or the variable containing the array. “$value” is a variable that stores the current element in each iteration.
Code & Discussion:
In this paragraph, we look at how the foreach() function works on an indexed array through its parameter and element-containing variables.
PHP Foreach() on Indexed/numeric arrays:
<?php
$colors = array("apple", "guava", "mango", "apricot");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
The output of the above code snippet would be:
Apple
guava
mango
apricot