php

Use of foreach loop in PHP

Use of foreach loop in PHP
Many types of loops are supported by PHP. foreach loop is one of them. This loop is mainly used to parse array and object variables. When the total number of array elements is undefined, then it is better to use a foreach loop than another loop. The number of iterations of this loop depends on the number of array elements or the number of properties of the object used in the loop for reading. How this loop can be used for reading array and object variables is shown in this tutorial.

Syntax:

foreach loop can be used for reading the array values only or both keys and values of the array.

The following foreach loop is used to read the element values of an array. It can be used to read both numeric and associative arrays. Each element value of the array will be stored in a variable in each iteration.

foreach ($array as $value)
//statements

The following foreach loop is used to read the element values of an array. It is mainly used to read associative arrays. In each iteration of the loop, each key and value of the array will be stored in two variables.

foreach ($array as $key => $element)
//statements

Example 1: Reading a numeric array

The following example shows the way to read the numeric array using a foreach loop. Create a PHP file with the following script.

A numeric array named $num_array of 10 elements is declared in the script, and an empty array named $new_array is declared to store the even numbers from the numeric array. foreach loop is used to iterate the numeric array, and if condition is used to find out the even numbers from the $num_array and store all even numbers into the $new_array. Another foreach loop is used to print the values of $num_array.

//Declare a numeric array
$num_array = array(12, 90, 15, 6, 32, 38, 75, 57, 60, 78);
//Declare an empty array
$new_array = array();
//initialize the index for the empty array
$index = 0;
echo "The array values are :
";
/* Iterate the numeric array to find out the numbers divisible by 5
and store in a new array */
foreach ($num_array as $value)
if ($value%5 == 0)

$new_array[$index] = $value;
$index++;

echo $value." ";

echo "

The list of numbers which are divisible by 5 :
";
//Print the values of the $new_array array
foreach ($new_array as $value)
echo $value." ";

?>

Output:

The following output will appear after running the script from the server. The output shows that there are four even numbers in the numeric array.

Example 2: Reading an associative array

The following example shows the way to read an associative array and store the particular keys from one array to another, using a foreach loop. Here, the associative array named $members contains the name of the member as key, and Present or Absent as the value of the array. An empty array named $present_member is used to store the name of the members who are present. Next, the foreach loop is used to print the values of $present_member.

//Declare an associative array
$members = array('Mehr Nigar' => 'Present', 'Ahmmed Ali' => 'Present', 'Maksudur Rahman' => 'Absent', 'Farzana Akter' => 'Present', 'Kamal Hasan' => 'Absent');
//Declare an empty array
$present_members = array();
//Initialize the index for the array
$index = 0;
//Find out the list of present memebers
foreach ($members as $key => $value)
if ($value == 'Present')
$present_members[$index] = $key;
$index++;


//Count the total numbers of present members
$count = count($present_members);
echo "$count members are present.

";
echo "The members are :
";
//Print the list of present members
foreach ($present_members as $value)
echo $value. "
";

?>

Output:

The following output will appear after running the script from the server. The associative array contains three Present values and two Absent values. For this, the output shows three members' names from the $members array who are present.

Example 3: Reading the properties of the object variable

The following example shows the way to read the property names and the values of any object. Create a PHP file with the following script.

A class named Customer is defined in the script that contains three public properties with values. Next, the $object variable is created to access the properties of the class. The object variable can be parsed like the associative array by using the foreach loop. The foreach loop is used here to read the property name and the property value of the object variable, $object. Each property name and value of the $object will be read in each iteration of the loop and printed in each line.

//Define the class with three properties
class Customer

public $name = 'Mehnaz Hossain';
public $email = '[email protected]';
public $phone = '0184423675';

//Create object of the class
$object = new Customer();
//Print the object properties using foreach loop
echo "

The customer details:

";
foreach($object as $property => $value)

echo "$property = $value
";

?>

Output:

The following output will appear after running the script from the server. The Customer class contains three properties, which are name, email, and phone. These properties are initialized with the values inside the class. The property names and values are printed in the output.

Conclusion

The various uses of the foreach loop in PHP are described in this tutorial by using multiple examples. The ways of reading array and object variables using the foreach loop are also shown.

This loop is better to use when the records are retrieved from any database table using PHP. This loop can be used to read only the array values or property values of the object and read the array values with keys or the property values with the names.

Gry SuperTuxKart for Linux
SuperTuxKart for Linux
SuperTuxKart is a great title designed to bring you the Mario Kart experience free of charge on your Linux system. It is pretty challenging and fun to...
Gry Battle for Wesnoth Tutorial
Battle for Wesnoth Tutorial
The Battle for Wesnoth is one of the most popular open source strategy games that you can play at this time. Not only has this game been in developmen...
Gry 0 A.D. Tutorial
0 A.D. Tutorial
Out of the many strategy games out there, 0 A.D. manages to stand out as a comprehensive title and a very deep, tactical game despite being open sourc...