How to Get Elements of an Associative Array by Index in PHP

Last Updated on January 3, 2021 by Roshan Parihar

In this tutorial, learn how to get elements of an associative array in PHP. The short answer is: use the index operator ([]) and pass the element key as the argument.

You can also use the PHP foreach loop or PHP for loop to access the elements of an associative array. However, the For loop requires to count the length of the associative array to parse all the elements.

The associative array is the array in which elements have a manually assigned key of string type. Each key is user-defined and users can prefer the way to declare the keys. The example of creating an associative array in PHP is as given below:

Let’s find out how to get elements of an associative array using PHP with the useful examples given below.

Get Elements of Associative Array Using Index Operator [] in PHP

To access the elements of an associative array, you have to use the index operator [] and pass the manually assigned key of the element as an argument. The below example showing the method of accessing the elements individually.

Output

2
5
9

The above example showing the output that contains all the items including the key and its relevant value with one element on a single line. There are 4 items with key and value pair.

Access Keys and Values Using Foreach Loop in PHP

When you want to parse through all the elements of an associative array, the PHP Foreach loop is the best method for anyone to traverse through each and every element. It traverses through all the elements with its key and value to print them one-by-one in the output. See the example below showing the method.

Output

Key is: Cycles, Value is: 2
Key is: Bikes, Value is: 5
Key is: Cars, Value is: 9

The above example showing all the items containing the key-value pair of the array that gets printed using the Foreach loop. This can be useful to get all the elements of the array at one go.

Find Associative Array Elements Using PHP For Loop

In addition to the above all methods, you can also use the for loop of PHP to iterate through all the items with key-value pair. It parses the key-value pairs of elements one-by-one and prints them in the output using the PHP echo function. See the example given below to learn the method on how to use the for loop method on an associative array.

Output

Key is: Cycles, Value is: 2
Key is: Bikes, Value is: 5
Key is: Cars, Value is: 9

The above example showing the for loop parses all the items with keys and values pairs. After that, it prints each one of them in the output with its key and value.

All the methods are useful in PHP to get the items of an array in PHP. You can use any of them to find the value of the matching keys in the output. However, the best method from the above examples is through Iterating that you will get by using PHP Foreach and PHP for loop.

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.