How to Loop Through an Associative Array Using PHP

Last Updated on January 13, 2021 by Roshan Parihar

In this tutorial, learn how to loop through an associative array elements in PHP. The short answer is: use the PHP Foreach loop or For loop to iterate through the elements.

The associative array contains elements in which all elements have a key that is manually assigned by the user. The key is of string type to declare the items. The example of the associative array in PHP is as given below:

You can also find the matching key and value if it is available in an associative array using the loop. You can use any of the loops from the foreach loop or the for loop in PHP that you can find in the examples given here.

Looping Through an Associative Array Using PHP Foreach Loop

To loop through all the elements of an associative array, you can use the foreach loop of PHP that requires arguments as the associative array variable and two other variables for key and value to get in the output. It traverses through all the elements one-by-one and prints them in the output as given below:

Output

Key is: Cycle, Value is: 2
Key is: Bike, Value is: 5
Key is: Car, Value is: 9

The above example showing the key with its relevant values in the output.

Iterate Over an Associative Array with For Loop in PHP

In addition to the above method, you can also iterate over all the elements of an associative array using the PHP For loop. However, it requires to find the length of an associative array using the PHP count() to perform the number of iteration. You also need to get the keys using array_keys() to store keys array in a variable as given below:

Output

Key is: Cycle, Value is: 2
Key is: Bike, Value is: 5
Key is: Car, Value is: 9

You will get the same elements as you have obtained using the foreach loop of PHP. The example contains all the items of the array in the output. However, for loop requires more coding as compared to the Foreach loop of PHP to iterate over associative array elements in PHP.

Get Matching Key and Value If Present Using PHP Loop

Suppose, you have a key that you have to check if matches in the associative array using PHP. You can use any of the above loops to get the matching key. However, it’s highly recommended to use the Foreach loop as requires less coding and faster as compared to the For loop of PHP.

You have to first store the given key in a variable that you have to match with all the items of the array. After that, use the foreach loop to parse through all the elements and use the PHP if…else condition to get the matching key and its value as given below:

Output

The key item ‘Bike’ is present in an associative Array with a value is 5

The above example shows that the given key is present in the associative array and its value is 5.

You May Also Like to Read

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.