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:
1 |
$myarrayassoc = array("Cycles" => 2, "Bikes" => 5, "Cars" => 9); |
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.
1 2 3 4 5 6 7 8 |
//declaring associative array $myarrayassoc = array("Cycles" => 2, "Bikes" => 5, "Cars" => 9); //Access elements of an associative array echo $myarrayassoc["Cycles"]; echo "<br>"; echo $myarrayassoc["Bikes"]; echo "<br>"; echo $myarrayassoc["Cars"]; |
Output
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.
1 2 3 4 5 6 |
$myarrayassoc = array("Cycles" => 2, "Bikes" => 5, "Cars" => 9); //traversing elements of an associative array foreach ($myarrayassoc as $key => $val){ echo "Key is: ".$key.", "."Value is: ".$val; echo "<br>"; } |
Output
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.
1 2 3 4 5 6 7 8 9 10 |
$myarrayassoc = array("Cycles" => 2, "Bikes" => 5, "Cars" => 9); //Get size of array elements $arrsize = count($myarrayassoc); //Return array keys in a variable $keys = array_keys($myarrayassoc); //Iterate elements of an associative array for ($x = 0; $x < $arrsize; $x++){ echo "Key is: ".$keys[$x].", "."Value is: ".$myarrayassoc[$keys[$x]]; echo "<br>"; } |
Output
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