Associative Arrays in PHP


Associative array will have their index as string so that you can establish a strong association between key and values. The associative arrays have names keys that is assigned to them.

Let us see an example−

$arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115");

Above, we can see key and value pairs in the array.

To implement Associative Arrays in PHP, the code is as follows −

Example

 Live Demo

<?php
   $arr = array( "p"=>"150", "q"=>"100", "r"=>"120", "s"=>"110", "t"=>"115", "u"=>"103", "v"=>"105", "w"=>"125" );
   echo "Value 1 = " .reset($arr);
?>

Output

This will produce the following output−

Value 1 = 150

Example

Let us now see another example −

 Live Demo

<?php
   $salaries = array("jack" => 2000, "qadir" => 1000);
   echo "Salary of jack is ". $salaries['jack'] . "
"; $salaries['jack'] = "high"; $salaries['tom'] = "low"; echo "Salary of jack is ". $salaries['jack'] . "
"; ?>

Output

This will produce the following output−

Salary of jack is 2000
Salary of jack is high

Updated on: 26-Dec-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements