This tutorial will discuss about unique ways to to loop through an array in php.
Table of Contents
Method 1: Using foreach()
We can use the foreach() method to iterate over all the elements in an array. In the foreach expression, we will assign each value of array to a variable and inside the foreach() body we will access each item of array using that variable.
In the below example we are going to loop through all the elements of array and print them one by one.
Let’s see the complete example,
<?php // Define an array of city names $cities = array( "New York", "Los Angeles", "Chicago", "Houston", "Philadelphia"); // Loop through the array foreach ($cities as $city) { echo $city . ", "; } echo "\n" ?>
Output
New York, Los Angeles, Chicago, Houston, Philadelphia,
Method 2: Using for-loop
In the previous example we iterated over all the elements of array and printed it. But we were not aware of the index position of the elements of the array.
We can use a for loop to iterate our all elements of array based on its index positions. For that, we will iterate from zero to N using a for loop. Where N is the size of the array and for each number i in the iteration, we can access the element with that index position “i”, and print it on console.
Frequently Asked:
- Check if all elements in Array are unique in PHP
- Check if array contains value greater than a number in PHP
- How to Loop through an Array in PHP?
- Check if a value exists in multidimensional array in PHP
Let’s see the complete example,
<?php // Define an array of city names $cities = array( "New York", "Los Angeles", "Chicago", "Houston", "Philadelphia"); // Loop through the array by index position for ($i = 0; $i < count($cities); $i++) { echo $cities[$i] . ", "; } echo "\n" ?>
Output
New York, Los Angeles, Chicago, Houston, Philadelphia,
Method 3: Using while loop
We can also use the while loop to iterate over each element of array. For this, we are going to use each() function, which returns all value sof array as the key and the value pairs. Where key is the index position and value is the item at that index position in array. But before using while loop with each() function, we need to call the reset() function with array. It resets the internal pointer to point to first element internally.
In the while loop expression we will keep iterating till the each function does not returns false.
Let’s see the complete example,
<?php // Define an array of city names $cities = array( "New York", "Los Angeles", "Chicago", "Houston", "Philadelphia"); // Reset internal pointer to point // to first element internally reset($cities); // Loop through the array while (list(, $city) = each($cities)) { echo $city . ", "; } echo "\n" ?>
Output
New York, Los Angeles, Chicago, Houston, Philadelphia,
Method 4: Using do while
The array_shift() function accepts an array as an argument and shifts the first value of array off. Basically it removes the first value and returns it. We can use it do while loop to iterate over all elements of array, till array is not empty.
Let’s see the complete example,
<?php // Define an array of city names $cities = array( "New York", "Los Angeles", "Chicago", "Houston", "Philadelphia"); // Loop through the array till array is not empty do { //shift the first value of the array off and fetch it $city = array_shift($cities); echo $city . ", "; } while (!empty($cities)); echo "\n" ?>
Output
New York, Los Angeles, Chicago, Houston, Philadelphia,
Summary
We learned about different ways to iterate over an array in PHP.