, , ,

How can we create foreach loop with a numeric array in PHP?

Posted by

Foreach loop in Php

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

guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x