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.

Example:-

<?php
    $age = array (
        "Ajay"=>10,
        "Vijay"=>20,
        "Sanjay"=>30,
    );
    echo $age["Ajay"] . "<br>";
    echo $age["Vijay"] . "<br>";
    echo $age["Sanjay"] . "<br>";
?>

Output:-

Associative arrays are written in two ways.

Type 1 :

<?php
    $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 />";
?>

Output:-

Type 2 :

<?php
    $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 />";
?>

Output:-

Related Posts

How to work in functions PHP?

If there is any task in your program which you train to execute repeatedly then instead of writing the code for that task at a different place…

For Loofs in PHP

In for-loop initialization, condition and increment happen on the same line. The loop keeps on repeating as long as the condition is true. When the for loop…

If Else Conditionals in Php?

If Else statement is the most important feature of any programming language, PHP is used to run any Code Of Block according to If Else conditions. This…

What are operators in PHP? PHP – Operator Types

Operators are used to developing data. In Programming Languages, Operators are taken from Mathematics and Operator Programmers work with Data. An operator is a chain of symbols…

String Functions in PHP?

When many characters are written together in sequence, they form a string. For example, India is a string. It is made up of i, n, d, i,…

What is the Variable And how does it work?

The variable is seen here as a container, which you can use to store data information. It doesn’t make any difference to him, so it becomes easy…

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