PHP Loop through array

There are some methods to look through one array in PHP. The functions are foreach, while and for.  In this article, we will show examples on PHP loop through array using each of these functions. By these examples, there will be a loop in the PHP script, and on each member of the array, the PHP script will show its value.

Instead, any operation can be performed for each member of the array found.

PHP Loop through array using foreach()

The foreach function is one of the most used for looping in PHP. As the name says: “For each” member, do such tasks.

Loop through a multidimensional array in PHP

The command foreach can be also used to look inside multidimensional arrays in PHP. Its usage will depend on the structure of the array.


So, the output:

In this example, $value on the first level of the array was brought as a new array. Then, you can run another foreach on this new array, to get its contents. We have used the is_array() PHP function to check whether it’s a simple string or an array.

More information about the foreach() functon can be found here.


Leave a Reply

Your email address will not be published. Required fields are marked *