Deva Point

deva point

Our Latest Programming Tutorial

Easy To Learning

PHP Foreach Loop

The foreach PHP construct is the most efficient method of iterating all elements of an array. It is a function that works with arrays and objects too. While the foreach loop is able to iterate through an array, its execution is streamlined and completes the loop faster relative to. This allocates memory to index iterations, which causes the entire system redundant in its performance, in terms of memory allocation.

This loops can be supported with PHP. foreach loop is just one of them. It is used primarily to process array and objects variables. When the amount of array elements is unknown, it is preferable to make use of a foreach loop instead of another loop. The number of iterations in this loop is determined by the amount of array elements, or the number of attributes of the object being it is reading. It is used to read objects and array variables.

PHP Foreach Loop Syntax

PHP foreach loops are useful to read values from the array alone or for both the keys and values of the array.

Foreach PHP loop in the following example is utilized to determine the elements from an array. It is used to read numeric and associative arrays. Each element’s value in the array will be saved in a variable for every iteration.

foreach ($array as value)

{foreach ($array as $value)

//statements

}

The following foreach loop can be utilized to determine the values of the elements in an array. It is mostly used in order to access associative arrays. Every time the loop, every value and key in the array will be recorded within two variable.

Foreach ($array with $key $element) {*

//statements

}

PHP foreach loop Examples

Example 1:

PHP program that prints array elements with foreach loop.

//declare array

“$day” = array (“Sunday”, “Monday”, “Tuesday”, “Wednesday”);

//access array elements by using a foreach loops

for each ($day as foreach ($day as) {foreach ($day as $element)

echo “$element”;

echo “”;

}

?>

Output:

Sunday

Monady

Tuesday

Wednesday

Example 2:

Use PHP foreach loop to prints associative array elements.

//declare array

The array is called $employee. (

“Name” => “Mark”,

“Email” => “mark21@gmail.com”,

“Age” => 21,

“Gender” => “Male”

);

//display array element associative through the foreach loop

each ($employee as $key $element) {foreach ($employee as $key = $element)

echo $key . ” : ” . $element;

echo “”;

}

?>

Output:

Name : Mark

Email : mark21@gmail.com

Age : 21

Gender : Male

Use of Multi-dimensional array in Foreach Loop

Foreach loop can use Multi-dimensional array to navigate through the array’s elements. It is only used on objects and arrays. It will throw an error when you try to use it with variables with different datatypes.

PHP foreach loop operates on the basis of elements, not an index. It is the simplest method of iterating all the components of an array.

PHP foreach loop

Example 3:

//declare multi-dimensional array

The array is $a.();

$a[0][0] = “Mark”;

$a[0][1] = “Jasmin”;

$a[1][0] = “Martin”;

$a[1][1] = “Anzola”;

//display multi-dimensional array elements via each loop

foreach ($a as foreach ($a as) {foreach ($a = $e1)

foreach ($e1 as foreach ($e1 as) {foreach ($e1 as $e2)

echo “$e2\n”;

}

}

?>

Output:

Mark Jasmin Martin Anzola

Published
Categorized as PHP

Leave a comment

Your email address will not be published. Required fields are marked *