How to use Associate Array in PHP

In this article, I will explain how the Associate Array can be used in PHP.
  • 2207

Array in PHP

  • An array is a data structure that stores one or more similar type of values in a single value.
  • Each element in the array has its own index so that it can be easily accessed.
  • An array in PHP is a collection of key/value pairs.

Associate Array

  • An Associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index.
  • An Associative array will have their index as string so that you can establish a strong association between key and values.
  • An Associative array, each ID key is associated with a value.
  • An Associative arrays we can use the values as keys and assign values to them.


Following is the example showing how to create and access Associate arrays.



<h2 style="color: blueviolet;">Associate Array example</h2>


    /* First method to associate create array. */

    $salary = array(

                 "Dinesh" => 20000,

                 "Nitin" => 15000,

                 "Manish" => 10000


    echo "Salary of Dinesh is ". $salary['Dinesh'] . "<br />";

    echo "Salary of Nitin is ".  $salary['Nitin']. "<br />";

    echo "Salary of Manish is ".  $salary['Manish']. "<br />";

    /* Second method to create associate array with key. */

    $salary['Dinesh'] = "High";

    $salary['Nitin'] = "Medium";

    $salary['Manish'] = "Low";

    echo "Salary of Dinesh is ". $salary['Dinesh'] . "<br />";

    echo "Salary of Nitin is ".  $salary['Nitin']. "<br />";

    echo "Salary of Manish is ".  $salary['Manish']. "<br />";






Associate array.jpg

You may also want to read these related articles here
Ask Your Question 
Got a programming related question? You may want to post your question here
© 2020 DotNetHeaven. All rights reserved.