Associative Arrays in PHP

PHPServer Side ProgrammingProgramming

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'] . "\n";
   $salaries['jack'] = "high";
   $salaries['tom'] = "low";
   echo "Salary of jack is ". $salaries['jack'] . "\n";
?>

Output

This will produce the following output−

Salary of jack is 2000
Salary of jack is high
raja
Published on 26-Dec-2019 07:39:11
Advertisements