PHP: foreach Loop :-

In the previous tutorials, we have discussed about other loops in PHP.Like for loop, while loop and do while loop. In this PHP tutorial, we will talk about foreach loop.The foreach loop is different from all the loops that work only on Array and used to loop through each key/value pair.

Foreach loop observes all key/value pair through pass the array as value. For example, if you have to fetch the values from an array, in the simple array you have to give index number 0,1,2,3,4, etc agina and again If there is more data add-on, then the index number has to be changed repeatedly. You can use foreach loop to remove this complexity. In Foreach loop the array has to be pass as a value.

Definition of foreach loop

The foreach loop is a very useful loop that accesses the array items one by one.The foreach loop work only on the arrays. The foreach loop some languages has some defined order, processing each item in the collection from the first to the last.

In simple words, We can use the foreach loop get data each row one by one.
For that, we pass the array as value inside the foreach loop .

It loops over the array, and each value for the current array element.

Syntax

This syntax helps to understand your foreach loop easily. The syntax of the foreach loop is very simple.

foreach
 (array as value) 
{ 
code to be executed; 
}


In the above syntax, we pass the array as value . It loops over the array and values of the current elements.

Examples of foreach loop

The following example of foreach loop, in which we will create an array and it's items and array pass as value in foreach loop for display the element one by one through the loop.

Example

<?php 
$student =array('Ram','Syam','Mohan','Geeta');
foreach($student as $all_names)
{
	echo $all_names."
"; } ?>
Run

In the above example, we create an array. We store some data inside the array.As you know, the array() function is used to create an array . After creating an array, we store the array in a variable. We pass the same variable as the value in foreach loop. Well, we do not need to give any index number. Foreach loop fetches all the values of the array by looping one by one.

Nested arrays with list() using foreach loop

In the example below, we create nested array. In this way you can easily create an Array . In this example, we pass the Array as a list(). In the latest version of PHP ability to iterate over an array of arrays and unpack the nested array into loop variables by passing array as list() inside foreach loop.The list works as the value .

Example

<?php 
$arr= [
    [6, 8, 0],
    [9, 4, 1],
];

foreach ($arr as list($x, $y,$z)) {
    echo "X: $x;  Y: $y; Z:$z
"; } ?>

Output

 

X: 6; Y: 8; Z:0
X: 9; Y: 4; Z:1

In the above example, we have created Array, After creating the array, the array has passed as list() in the foreach loop. Here, list() works for values . We create three variable $x,$y,$z .

$x Contains the first element of the nested array.

$y Contains the second element.

$z Contains the third element.