Foreach loop in PHP

 Foreach loop in PHP 

  • For traversing in the array elements use foreach. 
  • It works on array and object
  • Show an error if used with the different datatype variables.
  • This loop works on elements not the index and no need to increment the value. 
  • It provides an easiest and simple way to traverse the elements in the array.

Syntax

foreach ($array as $value) {  

   //code to be executed  

}  


For associative array.

Syntax

foreach ($array as $key => $element) {   

   //code to be executed  

}  


Flowchart

 


Example :

PHP program to print array elements using foreach loop.

1. <?php  

2.     //declare array  

3.     $season = array ("Summer", "Winter", "Autumn", "Rainy");  

4.       

5.     //access array elements using foreach loop  

6.     foreach ($season as $element) {  

7.         echo "$element";  

8.         echo "</br>";  

9.     }  

10. ?>  


Output:

Summer 

Winter 

Autumn 

Rainy


example:- 

<?php

$salary[]=2000;

$salary[]=3000;

$salary[]=5000;


foreach($salary as $value){

  echo "Salary: $value<br>";

}

?>


Output:- 

Salary: 2000

Salary: 3000

Salary: 5000


Example :

PHP program to print associative array elements using foreach loop.

1. <?php  

2.     //declare array  

3.     $employee = array (  

4.         "Name" => "Alex",  

5.         "Email" => "alex_jtp@gmail.com",  

6.         "Age" => 21,  

7.         "Gender" => "Male"  

8.     );  

9.       

10.     //display associative array element through foreach loop  

11.     foreach ($employee as $key => $element) {  

12.         echo $key . " : " . $element;  

13.         echo "</br>";   

14.     }  

15. ?>  


Output:

Name : Alex

Email : alex_jtp@gmail.com

Age : 21

Gender : Male


Example :

Multi-dimensional array

1. <?php  

2.     //declare multi-dimensional array  

3.     $a = array();  

4.     $a[0][0] = "Alex";  

5.     $a[0][1] = "Bob";  

6.     $a[1][0] = "Camila";  

7.     $a[1][1] = "Denial";  

8.       

9.     //display multi-dimensional array elements through foreach loop  

10.     foreach ($a as $e1) {  

11.         foreach ($e1 as $e2) {  

12.             echo "$e2\n";  

13.         }  

14.     }  

15. ?>  


Output:

Alex Bob Camila Denial


Example :

Dynamic array

1. <?php  

2.     //dynamic array  

3.     foreach (array (‘C, 'Y, 'B', 'E', 'R', ‘ ’, 'M', 'O', 'N', 'K') as $elements) {  

4.         echo "$elements\n";  

5.     }  

6. ?>  


Output:

CYBER MONK


iterate a constant array's value by reference:

<?php

foreach (array(1, 2, 3, 4) as &$value) {

    $value = $value * 2;

}

?>


Note:

foreach does not support the ability to suppress error messages using @.


/* example 4: multi-dimensional arrays */

$a = array();

$a[0][0] = "a";

$a[0][1] = "b";

$a[1][0] = "y";

$a[1][1] = "z";


foreach ($a as $v1) {

    foreach ($v1 as $v2) {

        echo "$v2\n";

    }

}


/* example 5: dynamic arrays */


foreach (array(1, 2, 3, 4, 5) as $v) {

    echo "$v\n";

}

?>


example:

<?php

$array = [

    [1, 2],

    [3, 4],

];


foreach ($array as list($a, $b)) {

    // $a contains the first element of the nested array,

    // and $b contains the second element.

    echo "A: $a; B: $b\n";

}

?>

output:

A: 1; B: 2

A: 3; B: 4


Example

<?php

$arr = array(2,4,6,8,10);

foreach ($arr as $i){

   echo $i . "*2=" . $i*2 . "\n";

}

?>


Output

2*2=4

4*2=8

6*2=12

8*2=16

10*2=20


Shows traversal of associative array with foreach loop

Example

<?php

$arr = array("Phy"=>50, "Che"=>60, "Maths"=>70, "Bio"=>80);

foreach ($arr as $sub=>$marks){

   echo "marks in $sub: " . $marks . "\n";

}

?>


Output

marks in Phy: 50

marks in Che: 60

marks in Maths: 70

marks in Bio: 80


A two dimensional array is traversed using nested foreach loops

Example

<?php

$arr1=[1,2,3,4,5];

$arr2=[6,7,8,9,10];

$twodim=[$arr1,$arr2];

foreach ($twodim as $row){

   foreach ($row as $col){

      echo $col . " ";

   }

   echo "\n";

}

?>


Output

1 2 3 4 5

6 7 8 9 10


Example

<?php

$arr1=["rno"=>1, "name"=>"Kiran", "marks"=>50];

$arr2=["rno"=>2, "name"=>"anil", "marks"=>60];

$arr3=["rno"=>3, "name"=>"Bina", "marks"=>70];

$twodim=[$arr1,$arr2, $arr3];

foreach ($twodim as $row){

   foreach ($row as $k=>$v){

      echo $k . ":" . $v . " ";

   }

   echo "\n";

}

?>

Output

rno:1 name:Kiran marks:50

rno:2 name:anil marks:60

rno:3 name:Bina marks:70

===================================================

Post a Comment

0 Comments