Php Loop Array

Often you need to move through all the elements in a PHP array so that you can do something with each element’s value. For example, you may want to display each value in an HTML table, or give each element a new value.

In Counting PHP Array Elements Using count(), I showed how you can use a for loop along with the count() function to loop through an array. However, there’s a much easier way to loop through arrays: the foreach construct.

In this article you learn the basic syntax of foreach, and see how to use it to loop through both indexed and associative arrays.

Looping through element values

If you push an array onto the stack, PHP will add the whole array to the next element instead of adding the keys and values to the array. If this is not what you want, you're better off using arraymerge or traverse the array you're pushing on and add each element with $stack$key = $value. Php $stack = array('a', 'b', 'c'). Complete PHP Array Reference. For a complete reference of all array functions, go to our complete PHP Array Reference. The reference contains a brief description, and examples of use, for each function! Let's start by filling an array with numbers from 1 to 100. We know the number of loop iterations, 100, so we will use the for loop. Its control variable ($i) will go from 0 to 99 (an array of 100 items will have indexes from 0 - 99). We'll have to increase the value by 1 because we want values from 1 to 100. Foreach loop through multidimensional array in PHP. Topic: PHP / MySQL Prev Next. Answer: Use the PHP nested loop. You can simply use the foreach loop in combination with the for loop to access and retrieve all the keys, elements or values inside a multidimensional array in PHP. Take care when replacing code using reset/next with code using foreach as foreach does not update the array's internal pointer. This means you cannot, say, use next to skip an element in foreach loop, or use current within a function to get a reference to the current element.

The simplest way to use foreach is when looping through the values in an indexed array. Here’s the basic syntax:

For example, the following script loops through a list of movie directors in an indexed array, displaying each director’s name as it goes:

The above script produces the following output:

Looping through keys and values

So far so good, but what about associative arrays? With these types of arrays, you often want to access each element’s key as well as its value. Luckily, foreach gives you a way to do just that:

Here’s an example that loops through an associative array of movie information, displaying each element’s key and value inside an HTML definition list:

This script displays the following:

Altering element values

So far you’ve just looked at reading element values, but what if you want to change values as you loop through? You might try something like this:

However, if you do this you’ll find that your array values aren’t changed. This is because foreach works on a copy of the array values, rather than the original array. So the original array remains untouched.

To change array values, you need to reference the values. You do this by placing an ampersand (&) before the value variable in the foreach construct:

$value then becomes a reference to the element value in the original array, which means you can change the element by assigning a new value to $value.

A reference is a pointer to the original value, rather than a copy of the value. It’s a bit like a shortcut in Windows, or an alias in Mac OS.


For example, the following script loops through each director in the $directors array, and uses PHP’s explode() function and list construct to reverse the director’s first and last names:

Php Loop Array

This script outputs the following:

Notice that the script calls unset() to delete the $director variable after it’s been used as a reference in the first loop. This is always a good idea if you think you might want to use the variable later in the script in a different context.

If you don’t delete the reference then you run the risk of accidentally referencing the last element in the array (“Lang, Fritz”) when you next use the $director variable, with often quite unexpected consequences!

In case you’re wondering, you can only change array values in a foreach loop. You can’t change array keys.


In this tutorial you’ve seen how to use PHP’s foreach construct to move through the elements in an array. You’ve looked at:

  • How to loop through array elements
  • How to access each element’s key and value within the loop
  • Using references to alter array values as you loop through the array

You can now easily create code that works on entire arrays, reading or changing array values as you go. Happy coding!

PHP, just like most other programming languages has multiple ways to loop through arrays. The most popular ways to do it usually is with a while, for and foreach operator, but, believe it or not, there are more ways to do it with PHP. In this article, I will walk-through each possibility for reading arrays whilst looping.

1. While Loop

The while loop is probably the most popular because of the recognizable and meaningful name. I always like to think of the while loop as the following.

Php for loop array

Whilst something is true, the loop will continue looping or vice versa.

Php Loop Array Key Value

This method is commonly used when working with database results in the style of arrays but at the same time, are a completely feasible way to read non-database result-arrays.

Iterate Through Array Php

Php Loop Array

With a plain array, we can create a true or false value depending on if the array has been looped over till the end. To achieve this, another variable must be created, an index indicator variable that does two things, allows us to pick out each of the values in the array as well as instructing the while loop when to stop. It’s somewhat very similar to how a for loop works, which we will touch on later.

PHP Code


2. For Loop

As demonstrated in the last section for while loops, you will see how similar the for loop is. It uses the very same concept for looping and picking out information from the array. Three parameters are needed for the for loop and they are as follows –

  1. An initial counter set to a certain value, usually zero.
  2. A Boolean test, usually involving the initial counter.
  3. A counter increment eg: counter++.

The start of the loop will always look similar to the following –

for ($i = 0; $i < count($arr); $i++)

What’s really important to note here is that you must use semi-colons after the first two parameters otherwise the code will throw an exception. Also, there is no need to increment the counter within the loop, as the for operator does this for you. Let’s put this loop into action in the following PHP.

PHP Code


3. Foreach Loop

This type of loop is my personal favorite way to read arrays, there is no need for a boolean test, just simply pass in the array and do what you want with it. It’s both easy to use, understand and comes in handy for many use-cases. There isn’t a mandatory rule to use a numeric index to pick out data values, the foreach loop essentially rids this concept for you. Let’s see it in action –

PHP Code


As you can see within the code example, there is much less fluff in terms of code. And, if you name your variables well, the code will make a lot more sense than other looping methods.

4. Do While Loop

You could almost say the do while loop is pretty scarce among PHP code, I personally rarely use it and similarly rarely see it from other peoples code neither. It’s essentially a longer or code-bulkier way of executing a while loop, which in a way, is pretty pointless. At the same time, it’s arguably more readable, but ultimately, it all comes down to preference.

With the do while loop, you will need to create a test within the while operator, just as you would in a pure while loop case.

PHP Code

Php Foreach Array


As you can see in the code, there is a need to create an increment index value that will work part as the test and part to access values from the array.

5. Array Iterator

Now, this is a slightly more advanced method of looping over arrays is called an ArrayIterator. It’s part of a wider class that exposes many accessible variables and functions. You are more likely to see this as part of PHP classes and heavily object-oriented projects. Again with this functionality, you may find it more preferable to others or vice versa. The documentation on this class is well worth eye-balling as you can see exactly what benefits it offers. For example, it instantly exposes a count of the array which is quick and useful.

PHP Code

Php Loop Array Backwards


Php loop arrays

Function References

Here are the links to the documentation on each of the loops used within this article. Check them out as they are extremely informative and useful for bookmarking for reference later.


With the demonstration of five ways to loop over arrays, you have the luxury of picking a preferred and truly personal way of doing things yourself. In some cases, one will perform better than the other with regards to what you need to achieve. I hope this comes in useful, now go do some cool stuff with some loops! Finally, feel free to copy any of the code in this article to use in your own projects or merely reference.