PHP Foreach

The foreach statement is a language construct that will iterate over the associative array data type. It was introduced in PHP 4.

Although it can also be used to iterate through an object’s property names, this article will focus exclusively on iteration over numeric and string arrays.

Since its inception, foreach has been widely criticized by developers for being slow compared to explicit for loops. In version 7, however, new internal implementations of both arrays and objects have raised the performance bar quite a bit.

PHP is a language that requires you to be very explicit about your data types. Because of this, it can be hard for newcomers to get the hang of.

For example, when using foreach loops with both arrays and objects (which use reference variables in PHP) the rules are different: in one case we go by position and in the other by key.

Also, whereas an associative array’s keys must be strings, indexed arrays’ keys may either be integers or strings. They’re automatically converted into integers if they’re not already. The examples below illustrate correct usage for each kind of looping construct.

PHP foreach with indexed arrays

As the title suggests, PHP foreach syntax with an indexed array is a bit different from any other function you can use with it. The main difference is that when you iterate over an indexed array, each element in the array will be assigned to a local variable automatically.

For example, there might be a list of colors that you want to switch between red, blue, and green. This can be done with the following foreach loop:

<?php

$colors = ["red", "blue", "green"];

foreach ($colors as $color) {
	echo "This is color: " . $color.'<br/>';
}

The above loop would output the following:

This is color: red
This is color: blue
This is color: green

PHP foreach with an associative array

In a previous section, I introduced the concept of using PHP’s foreach loop to iterate over each element of an array one by one. This is a very good way to do things if your array only contains simple variables as elements.

But what happens if you have something more complex as an element? In this case, you have to use a different foreach loop syntax, called an “indexed” foreach. An example of this is the following snippet:

<?php
$array = array('first' => 'Hello', 'second' => 12);
foreach( $array as $key => $value ) { 
	echo "$key => $value <br />" ; 
}

As you can see, the syntax is pretty similar to normal foreach, except that you now have two variables called $key and $value.

The key variable represents the index of the element at that point – this enables us to cycle through every single element in the array.

The value variable represents each element’s value – this enables us to see what the value of the element is. Thus, for example, if we run through that snippet above, we would get something like this:

first => Hello
second => 12
About Ashis Biswas

A web developer who has a love for creativity and enjoys experimenting with the various techniques in both web designing and web development. If you would like to be kept up to date with his post, you can follow him.

Leave a Comment