Please note, this is a STATIC archive of website technosmarter.com from 20 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

Foreach Loop


In the previous tutorials, we have discussed loops in PHP.Like for loop, while loop and do while loop. In this PHP tutorial, we will talk about the 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 again and again If there is more data add-on, then the index number has to be changed repeatedly. You can use the foreach loop to remove this complexity. In Foreach loop the array has to be pass as a value.

Definition of the 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 to get data each row one by one.
For that, we pass the array as the 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 the 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 the 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 the 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.


Please Share

Recommended Posts:-