The
for each loop provides an easy way to iterate over arrays. For each 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 for each loop it will
continue until it has gone through every item in the array. For each loop only
used when we have an array, without array it will not work.
There
are some advantage of using for each loop also, when for each 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 for each loop.
There are two
ways to write for each 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
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 for each loop in PHP
Using
for each loop on multi-dimensional array:
Suppose we have a courses array and that have three more arrays
inside it.
$courses = array(
'graphics'=>array('Photoshop',
'GIMP', 'Illustrator', 'CorelDraw'),
'web'=>array('HTML',
'JavaScript', 'PHP', 'Python'),
'animation'=>array('Maya',
'Cinema 4D', 'AfterEffects', '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 multidimensional 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 for each loop of
PHP with single and
multi-dimensional array.
The for each loop statement only works with array and
object. If we try to use for each loop with other data type we will get an
error message.
No comments:
Post a Comment