PHP foreach

Summary: in this tutorial, you will learn how to use PHP foreach loop statement to loop over elements of an array or public properties of an object.

PHP foreachPHP provides the foreach loop statement that allows you to iterate over elements of an array or public properties of an object. The foreach loop statement only works with arrays and objects. If you use the foreach loop statement with other data types, you will get an error.

PHP foreach – looping over elements of an indexed array

To loop over elements of an indexed array, you use the following syntax:

PHP assigns each element of the indexed array to the $element variable in each iteration. See the following example:

In the example, we assigned each element of the $scores array to the $value variable and displayed values of elements in each iteration.

To change the values of array elements inside the loop, you have to use a reference that returns by the foreach loop. To enable the foreach loop to return a reference to the array element, you add an ampersand ( &) symbol in front of the loop variable as follows:

The following example demonstrates how the change values of elements of an indexed array inside the loop:

In this example, we iterated over the elements of the $scores array and double the value of each element inside the loop.

Using a reference with the  foreach loop could be problematic. See the following example:

In both  foreach loops, it seems that we did not change the elements of the $scores array. However, after the second loop, the $scores array is:

The reason is after the first loop, the variable $score always referenced to the third element of the $scores array.

In the first iteration, we assigned $score the first element of the $scores array and because  $score is pointing to the third element of the $scores array, therefore, the first iteration is equivalent to:

Therefore, after the first iteration the content of the $scores array is:

In the second iteration, we assign $score the second element of the $scores array and keep in mind the $score is still pointing to the third element of the $scores array, therefore, the second iteration is:

So after the second iteration, the $scores array is:

In the third (last) iteration, we assigned the third element of the $scores to itself:

Therefore, the content of the $scores array is:

I hope this explanation helps you understand the problem with reference variable in the  foreach loop.

To fix the problem, you should always reset the reference variable after the loop using the unset() function as follows:

PHP foreach – iterate over elements of an associative array

To loop over elements of an associative array, you use the following syntax:

For each element in the array, the key is assigned to the $key variable and the value is assigned to the $value variable.

Let’s see the following example:

PHP foreach – iterate over public properties of an object

The syntax of iterating over the public properties of an object using  foreach loop is the same as iterating over the elements of an associative array.

See the following example:

In this example:

  • First, we defined Person class that has three public properties: firstName, lastName and middleName.
  • Second, we created a new object of the Person class named $john.
  • Third, we used the foreach statement to loop over the public properties of $john object and displayed their values.

In this tutorial, you have learned how to use the PHP foreach statement to iterate over elements of indexed arrays, associative arrays, and public properties of an object.

  • Was this tutorial helpful ?
  • YesNo