Associative Arrays in php

What is Associative array Or Key Paired array in PHP ?

Associative arrays are arrays that use strings as index assign by you .This stores element values in association with key values rather than in a conventional linear index order.Associative array will have their index as string so that you can establish a strong association between key and values. Associative arrays used like a structure by specify string key instread of numeric index.

eg. ‘Key’ =>’value’  , where ‘=>’ is key value operator

There are two ways to create an associative array:

  1.  $population = array(“India”=>”100”, “China”=>”120”, “USA”=>”90”);
  2. $population [‘India’] = “100”;
    $population [‘Ben’] = “120”;
    $population [‘USA’] = “90”;

In both the condition we are storing the value with respect to country in $population array. You can use any one of them as required .

Note: In Associative arrays index would not work in place of string key.it means $population[1] will not provide any result.

How to Use Associative arrays ?

As we seen above how to create or store value in associative array, now if we want to use array we have to use foreach loop to fetch values from array .

like for an example

foreach($population  as $key=>$value)

{

echo”$key =$value </br>”

}

Working with associative array can also be done with Stack implementation , With the using of stack in array you can add or delete element in array.