Associative Array in PHP?

An associative array is a simple and very important array of PHP. This array is also used the most, it is very easy to understand this array, and it contains keys and values. In simple Hindi language, key works like name and value work as information related to that name. For Mohan is a key and Mohan’s age or address, email, etc are its values.

With the help of an associative array, you can assign any value to any key. Associative array depends on key and value (=>).

An associative array is the best array in PHP. An associative array is used to store key => value pair. We define a key for value. At the time of execution (at run time), we can get the value related to the key by using the key.

PHP Associative Array Syntax

$x = array('key1'=>'value1', 'key2'=>'value2');
$x = ['key1'=>'value1', 'key2'=>'value2'];

Difference Between Indexed Array And Associative Array

The biggest difference between an Indexed Array and an Associative Array in PHP is that in an Indexed Array we only have to insert values so that value is associated (bind) with an indexed number, whereas in an Associative Array we insert value with a key so that The value is associated only with the given key.


    $age = array (
    echo $age["Ajay"] . "<br>";
    echo $age["Vijay"] . "<br>";
    echo $age["Sanjay"] . "<br>";


Associative arrays are written in two ways.

Type 1 :

    $percentages = array(
                     "Raj" => 54, 
                     "Prakash" => 85, 
                     "Narayan" => 60 

    echo "Raj got ".$percentages["Raj"]. " percentages. <br />";
    echo "Prakash got ".$percentages["Prakash"]. " percentages. <br />";
    echo "Narayan got ".$percentages["Narayan"]. " percentages. <br />";


Type 2 :

    $percentages["Raj"] = 54; 
    $percentages["Prakash"] = 85;
    $percentages["Narayan"] = 60;

    echo "Raj got ".$percentages["Raj"]. " percentages. <br />";
    echo "Prakash got ".$percentages["Prakash"]. " percentages. <br />";
    echo "Narayan got ".$percentages["Narayan"]. " percentages. <br />";


Related Posts

PHP explode() Function Example Tutorial

A PHP explode function example is given in this article. I will demonstrate how to use PHP’s explode string to array function. This is a basic example…

How to Convert Array to String in PHP?

We will cover the example of converting an array to a string in PHP in this lesson. We’ll examine an example of converting an array to a…

How Can I Convert a String in PHP Into an Array?

We will go over how to transform a string into an array in PHP in this little tutorial. Describe how to convert a string value into an…

How to Use the array_merge() Function in PHP?

We will go over the simple and quick method to use the PHP array_merge() function example in this brief tutorial. Let’s talk about the PHP example of…

How to Merge Two Arrays in PHP?

I explained simply how to merge two arrays in PHP Laravel. you will learn how to append two arrays in PHP. you will learn how to merge…

How to Get the First 5 Elements of Array in PHP?

Here, I will show you the php array to get the first 5 elements. let’s discuss about php array getting 5 first element values. you can see…

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x