Array traversal with loops (foreach)
In PHP programming, traversing or looping through an array’s data from the start element to the end element means processing and managing each array element by looping through it to access its values or modify the array’s data elements. The most common method for traversing an array’s data elements in PHP is using a foreach loop. The foreach loop helps complete the array traversal process and can be used with indexed and associative arrays.

Basic foreach loop in PHP.
In PHP programming, the foreach loop allows the programmer to traverse each element of an array, providing the programmer with direct access to the array’s values and array element keys in an optional order. There are two common methods for applying the foreach loop in PHP.
Two common methods for applying a foreach loop.
- Values only – This method iterates over the values of the current array element.
- Keys and values - This method iterates over both array keys and element values in an existing associative array.
Example of traversing an indexed array (values only) in PHP.
<?php
$countries = [“India”, “Usa”, “Chiha”, “Uk”, “Australia”];
foreach ($countries as $country) {
echo $country. “\n”;
}
?>
In the indexed array (values only) example.
- In this program, programmers apply a foreach loop to loop through each element of an indexed array named $countries.
- In this, the variable $country represents each array value in the array during each array element repetition.
Example of traversing an indexed array (key and value) in PHP.
<?php
$countries = [“India”, “Usa”, “Chiha”, “Uk”, “Australia”];
foreach ($countries as $key => $country) {
echo “Index $key: $country \n”;
}
?>
Indexed array (key and value) condition.
- In this program, $key indicates the reference to the index element (e.g., 0, 1, 2, 3, 4, etc.).
- The $country variable represents the value at each array index.
Traveling an associative array in PHP.
In PHP programming, an associative array is a storage location where each array element stores key data and holds a value. The foreach loop allows programmers to easily access and process both array data and array values during repetition.
Example of traversing an associative array (key and value).
<?php
$student = [
“stu_name” => “Harry”,
“stu_age” => 27,
“stu_cont” => 9414,
“country” => “Florida”
];
foreach ($student as $key => $stu_info) {
echo “$key – $stu_info \n”;
}
?>
In the example of traversing an associative array.
- In this program, the loop iterates over each key-value pair in the $student associative array.
- Here, $student represents the $key keys (e.g., “stu_name”, “stu_age”, “stu_cont”, “country”).
- And the $student associative array represents the $student_info values (e.g., “Harry”, 27, “9414”, “Florida”).
Nested foreach loop for multidimensional array in PHP.
In PHP programming, if the programmer is dealing with a multi-dimensional array (an array within an array), then PHP programmers can apply a nested foreach loop to access and process the multi-dimensional internal array element data.
Example of traversing a multidimensional indexed array.
<?php
$table = [
[9, 4, 1, 3],
[1, 9, 7, 5],
[2, 6, 5, 4]
];
foreach ($table as $rows) {
foreach ($rows as $values) {
echo $values . ” “;
}
echo “\n”;
}
?>
In the multidimensional indexed array condition.
- The outer foreach loop iterates over the data elements in rows, each of which is a multi-dimensional array.
- In this, the foreach loop in the internal array iterates over the values element of each row in the multi-dimensional array.
Example of traversing a multidimensional associative array in PHP.
<?php
$student = [
“Harry” => [“contact” => “111-222-333”, “id” => “101”, “email_id” => “harry@domain.com”],
“Sid” => [“contact” => “444-555-666”, “id” => “102”, “email_id” => “sid@domain.com”]
];
foreach ($student as $stu_name => $stu_info) {
echo “$stu_name’s Student Detail \n”;
foreach ($stu_info as $key => $values) {
echo “$key = $values \n”;
}
echo “\n”;
}
?>
In the multidimensional associative array example.
- In this program, the outer foreach loop iterates over each student array element (“Harry” and “Sid”).
- Similarly, the inner foreach loop iterates over each array key-value pair within the student array details (e.g., “contact”, “id”, “email_id”).
Modifying array elements using a foreach loop in PHP.
In PHP programming, programmers can also modify array element data directly within a foreach loop if needed. Remember, if programmers want to modify the original array, they must use references to those arrays.
Example of modifying array values using references in PHP.
<?php
$integers = [7, 9, 8, 10, 12];
foreach ($integers as &$integer) {
$integer *= 3; // here it Multiply each integer element with 3 numbers
}
print_r($integers);
// Result – ( [0] => 21 [1] => 27 [2] => 24 [3] => 30 [4] => 36 )
?>
Modifying array values using references in the condition.
- In this program, programmers use &$integer to reference array element data, allowing them to modify the original array value if needed.
- Loop Important – Be careful when using the & operator with foreach in PHP programming, as it directly modifies the original array data type value. After using the foreach loop, be sure to use the unset method to avoid abnormal system Behavior.
unset($number);
Skipping or Breaking a Loop in PHP.
In PHP programming, programmers can use the continue keyword to proceed to the next iteration, or use the break keyword to completely exit the current program loop.
Example of using continue in foreach in PHP.
<?php
$phones = [“apple iphone”, “motorola”, “samsung”, “nokia”, “oppo” ];
foreach ($phones as $phone) {
if ($phone == “samsung”) {
continue; // here it skips when it reaches the “samsung” element
}
echo $phone . “\n”;
}
?>
Using continue in foreach In the example,
The continue statement in this program skips the iteration when it reaches the “samsung” array element, moves to the next item in the $phones array and displays the array element.
Example of using break in foreach in PHP.
<?php
$phones = [“apple iphone”, “motorola”, “samsung”, “nokia”, “oppo” ];
foreach ($phones as $phone) {
if ($phone == “nokia”) {
break; // here it stops the loop when it reaches the “nokia” element
}
echo $phone . “\n”;
}
?>
In the example using break in foreach.
- In this program, the foreach loop break statement exits the loop when the “nokia” element is reached in the $phones array, and the remaining statements are not printed.
A summary of the use of the foreach loop in array traversal in PHP.
- Indexed arrays – Iterate over indexed array values or manage access to both array keys and values.
- Associative arrays – In associative arrays, iterate over key-value pairs in array element data.
- Multidimensional arrays – You can use a nested foreach loop to iterate over array elements within a multi-dimensional array.
- Modifying an array – In PHP programming, use the reference and (&) operator to modify the original array.
- Quitting/Breaking – In PHP programming, use the continue keyword to ignore repetitions on array data elements, and use the break keyword to stop a foreach loop.