We use cookies on this site to enhance your user experience

By clicking the Accept button, you agree to us doing so. More info on our cookie policy

The PHP foreach loop

PHP Fundamentals

Published: Oct 9, 2021 by C.S. Rhymes

PHP Fundamentals

This post is part of a series of posts about the fundamentals of PHP.

An alternative to the for loop, the foreach loop is used to iterate or loop over an array. The foreach loop allows you to loop through the items in the array without setting a limit for it to stop like you do in a for loop.

Once the foreach loop has looped through every item it will finish. If there are ten items in the array, then it will loop ten times.

Basic Foreach Loop

The foreach loop accepts an array as the first argument and then you give a name for the individual array item.

  • In the example below we are passing in $trees (the array) as the first argument.
  • We setting the individual array item with as $tree that can be used within the foreach loop.
  • If you don’t set both arguments then you will get a syntax error.
  • If the first argument isn’t an array then you get an invalid argument warning.
$trees = ['oak', 'ash', 'birch', 'maple'];

foreach ($trees as $tree) {
    echo $tree . ' ';
}

// oak ash birch maple

Breaking out of a foreach loop

If you want to, you can break out of a foreach loop rather than looping through every item in the array by using the break keyword. In the following example we check if the $tree is equal to ash then break the loop.

$trees = ['oak', 'ash', 'birch', 'maple'];

foreach ($trees as $tree) {
    echo $tree . ' ';

    if ($tree === 'ash') {
        break;
    }
}

// oak ash

Using the index in the Foreach loop

You can also use the index in the foreach loop. Here we have an array with the tree names as the key and a count as the value. The first argument is the array, then after the as you have a syntax that looks similar to the array syntax, as $key => $value where the first variable is the array key and the second is the array value.

$trees = [
    'oak' => 4, 
    'ash' => 3, 
    'birch' => 8, 
    'maple' => 6,
];

foreach ($trees as $tree => $count) {
    echo "There are $count $tree trees";
    echo '</br>';
}

// There are 4 oak trees
// There are 3 ash trees
// There are 8 birch trees
// There are 6 maple trees

Modifying the original value

If you want to modify the values in an array you can use a foreach loop to do this by putting the & before the second argument. In this example we add 1 to each of the original values in the array.

$myValues = [1, 2, 3, 4];

foreach ($myValues as &$myValue) {
    $myValue = $myValue + 1;
}

print_r($myValues);

// Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 )

There are also other PHP array methods that can be considered for such a use case instead of foreach, such as array_map() and array_walk()

Foreach loop with an array of objects

So far we have talked about an array, but what about an array of objects?

Here we have a Tree class or object that has name and count properties. The constructor sets these properties when a new Tree() class is created.

class Tree
{
    public $name;
    public $count;

    public function __construct($name, $count)
    {
        $this->name = $name;
        $this->count = $count;
    }
}

Next we can create an array of these Tree classes and then loop over the array. Rather than having to use the index within the foreach loop, the tree name we can access the Tree’s properties (count and name) using the arrow syntax, such as $tree->count.

$trees = [
    new Tree('oak', 4),
    new Tree('ash', 3)
];

foreach ($trees as $tree) {
    echo "There are $tree->count $tree->name trees";
    echo '<br>';
}

There are lots of use cases for a foreach loop so hopefully this introduction will give you ideas to put the foreach loop to use in your code.

PHP Beginner Code

Latest Posts

Five book blurb writing observations
Five book blurb writing observations

Amazon KDP gives you a basic text editor for your book’s blurb, but here are five observations that I have made from researching other books. All of the examples are taken from Mystery books in the Amazon UK store.

Creating a Kindle formatted book from Google Docs
Creating a Kindle formatted book from Google Docs

I have seen a few posts on Threads recently asking what software people use to format their books. This is one option out of many, but I thought I would share my current workflow to give authors an insight into the pros and cons.

Hosting a Laravel app with AWS Beanstalk
Hosting a Laravel app with AWS Beanstalk

There are lots of possible hosting solutions available for Laravel, from Forge, to Vapor to the new Laravel Cloud. I’ll start out by saying that these other solutions are much easier to get up and running than beanstalk, but I thought I’d share some of the “fun” I had getting it up and running.

How NOT to make a website

Unlooked for Tales - a collection of short stories

By C.S. Rhymes

Free on Apple Books and Google Play Books

Nigel's Intranet Adventure

Nigel's Intranet Adventure

By C.S. Rhymes

From £0.99 or read for free on Kindle Unlimited!