The foreach loop provides an easy way to iterate over arrays. This loop works only on arrays and objects. As we know that other loops like for loop or while loop continue until some condition fails but in case of foreach loop it will continue until it has gone through every item in the array. Foreach loop only used when we have an array, without array it will not work.
When you learn PHP course then you have been told about some advantage of using foreach loop also, when foreach first start to executing, the internal array pointer is automatically reset to the first element of the array. We do not need to call reset() before a foreach loop.
There are two ways to write foreach loop:
Syntax 1:
foreach ($array as $value) { Code to be executed; }
Here the value of the current element is assign to $value and the internal array pointer is advanced by one.
Example
<!doctype<!DOCTYPE html> <html> <head> <title>foreach loop testing</title> </head> <body> <?php $web=array("html","css","javascript","php"); foreach($web as $value) { echo "$value<br>"; } ?> </body> </html>
Output:
html
css
javascript
php
Syntax 2:
foreach ($array as $key => $value) { //code to be executed }
In this form it will additionally assign the current element key to the $key variable on each iteration.
Example
<!DOCTYPE html> <html> <head> <title>foreach example</title> </head> <body> <?php $studentinfo = array ( "name"=>"lakshminandon", "email"=>"lnandon108@gmail.com", "age"=>18 ); foreach ($studentinfo as $key => $value) { echo $key.":".$value."<br>"; } ?> </body> </html>
Output:
name:lakshminandon
email:lnandon108@gmail.com
age:18
Some more examples of foreach loop in PHP
Using foreach loop on multi-dimensional array
Suppose we have an courses array and that have three more array inside it.
$courses = array( 'graphics' => array('Photoshop', 'GIMP', 'Illustrator', 'CorelDraw'), 'web' => array('HTML', 'JavaScript', 'PHP', 'Python'), 'animation' => array('Maya', 'Cinema 4D', 'After Effects', 'Final Cut Pro') );
What if we want to print all the courses in table with serial number in each course group. We must understand the use of list() function of PHP.
list() function lists all the values from the concern array one by one.
See the below given example please.
foreach ($courses as list($a, $b, $c, $d)) { // $a contains the first element of the nested array, // and $b contains the second elements and so on echo "A: $a; B: $b; C:$c; D:$d"; }
If you want to use the list for multi-dimension arrays, you can nest several lists:
<?php $array = [ [1, 2, array(3, 4)], [3, 4, array(5, 6)], ]; foreach ($array as list($a, $b, list($c, $d))) { echo "A: $a; B: $b; C: $c; D: $d;<br>"; }; ?>
Will output:
A: 1; B: 2; C: 3; D: 4;
A: 3; B: 4; C: 5; D: 6;
And:
<?php $array = [ [1, 2, array(3, array(4, 5))], [3, 4, array(5, array(6, 7))], ]; foreach ($array as list($a, $b, list($c, list($d, $e)))) { echo "A: $a; B: $b; C: $c; D: $d; E: $e;<br>"; };
Will output:
A: 1; B: 2; C: 3; D: 4; E: 5;
A: 3; B: 4; C: 5; D: 6; E: 7;
Conclusion
Here in this article you learn how to use foreach loop of PHP with single and multi-dimensional array.
The foreach loop statement only works with array and object. If we try to use foreach loop with other data type we will get an error message.
View Full Presentation on slideshare: