PHP Associative Array

Associative Array is a simple and very importable array of PHP. With the help of Associative Array, you can assign any value to any key. Associative array depends on the key and value(=>) method. In the associative array, key assign to values. We have discussed in previous tutorials about Arrays , Index Arrays, and two-dimensional arrays. The index array contained numeric index numbers, whose help to find the value of an index, but the associate array is quite different from the index array.

Definition and use of the Associative array

The Associative Array is one of the efficient arrays in PHP. The associative array is used to store in the key-value pair. In simple word, it is used to store the value using key and value pair
We define the key for the value. At the time of execution, we pass the key and It gives the related value as output .

Syntax of Associative array

The following syntax of the associative array will help you understand the key pair concept.


In the above syntax, you can see that the key is making pairs with value using => sign. To make a pair of key and value, we use is equal to(=) and greater than (>) sign. The sign (=>) is applied from the key to value. This shows that you have assigned the key to the value.

Examples of Associative Array

The following example will help you understand the complete concept of associative. array


Marks : <?php echo"$number[vishal]"; 
Marks :<?php echo "$number[Manish]"; 
Marks :<?php echo "$number[Pankaj]"; 
Marks :<?php echo "$number[Hemant]"; 





In the above example, we have four students, names are Vishal, Manish, Pankaj, Hemant.
We have to find out the Marks of one by one student but we do not know about Vishal, Manish, Pankaj, Hemant roll numbers so we create the keys like Vishal, Manish, Pankaj, Hemant.
…Means - Vishal is a key, Manish is also a key, Pankaj is also a key, Hemant is also key and associates with values.

This key is related to marks (value). According to the associative array, a student is a key and number is value.

Hemant(Key)=>89 (value) .

After that, we find the marks by the name (key) we do not need any roll number to find out the marks.

We pass the name of the student only and after execution, we got associate value(marks) of students.
This is the major advantage of the associative array. Key=>value . Key associates with value.

Two dimensional array with associative array-

As we have already discussed the associative array in the two-dimensional array and multidimensional array, we also created examples using the associative array. The array() function is also used to create the associative array In the following example, we store data for products. In the product, we store some data such as product price, product discount and product quantity. Here we use the key-value pair to store the data. Here we have three customers. Those who have bought the product indifference price and quantity. The three also get some discounts. Now, create a two-dimensional array with the associative array.

                  $products= array( 
            "Renu" => array (
               "quantity"=> 2,
               "discount"=> 20,
               "price" => 546
            "Geeta" => array (
              "quantity" => 1,
               "discount" => 10,	
               "price" => 320
            "Raman" => array (
               "quantity" => 4,
               "discount" => 60,	
               "price" => 1200
echo "Renu got a discount = ".$products['Renu']['discount']."%";
"; echo "Gita bought product in rupees = ".$products['Geeta']['price']; echo"
"; echo "Raman bought product in quantity = ".$products['Raman']['quantity']; ?>
Renu got a discount = 20%
Geeta bought product in rupees = 320
Raman bought product in quantity = 4

In the above example, we have stored separate information for all three customers.

Renu got discount .

Geeta bought the product at how much price.

Raman bought the product at how much Quantity.

According to the two dimensional aaray each element of the main array can be an array and the sub-element of the sub-array can be an array. It is the same process for 3,4,5, dimensional array. That is known as a multidimensional array.

According to associative array , we assined key with value .


 quantity=> 2
 discount=> 20
 price => 546

Quantity is a key and the value for key is 2 .

discount is also a key and the value for key is 20 .

price is a key and the value for key is 546.

Follow this process the same for Geeta and Raman. At the end , we got the - Renu got a discount = 20%, Geeta bought product in rupees = 320, Raman bought product in quantity = 4 using key and value pair.

Please Share

Recommended Posts:-