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.
//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.
//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 : <br />";
/* 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 "<br /><br />The list of numbers which are divisible by 5 : <br />";
//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 "<b>$count members are present.</b><br /> <br />";
echo "<b>The members are : </b><br />";
//Print the list of present members
foreach ($present_members as $value) {
echo $value. "<br />";
}
?>
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 = 'mehnaz@gmail.com';
public $phone = '0184423675';
}
//Create object of the class
$object = new Customer();
//Print the object properties using foreach loop
echo "<h3>The customer details: </h3>";
foreach($object as $property => $value)
{
echo "$property = $value <br />";
}
?>
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.
from Linux Hint https://ift.tt/2MaUqwr
0 Comments