Signup/Sign In

PHP Associative Arrays

An associative array is similar to an indexed array, but rather than storing data sequentially with numeric index, every value can be assigned with a user-designed key of string type.


Creating an Associative Array

Just like indexed array, there are two different ways of creating an associative array in PHP, they are,

Syntax for the 1st way to create associative array:

<?php
/* 
    1st - direct array initialization
*/
$lamborghinis = array("suv"=>"Urus", "sports"=>"Huracan", "coupe"=>"Aventador");

?>

and the syntax for the 2nd way to create associative array is:

<?php
/* 
    2nd - distributed array initialization
*/
$suzuki["hatch"] = "Swift";
$suzuki["utility"] = "Ertiga";
$suzuki["suv"] = "Vitara";

?>

No matter how we initialize the array, we can access the array as follows,

<?php
/* 
    Accessing array
*/
echo "Accessing the 2nd array...";
echo $suzuki["hatch"], "\n";
echo $suzuki["utility"], "\n";
echo $suzuki["suv"], "\n";

echo "Accessing the 1st array...";
echo $lamborghinis["suv"], "\n";
echo $lamborghinis["sports"], "\n";
echo $lamborghinis["coupe"], "\n";

?>

Accessing the 2nd array... Swift Ertiga Vitara Accessing the 1st array... Urus Huracan Aventador

Hence, to access an associative array, we have to use the array name along with the index of the element in square brackets, the only difference here is that the index will be a string not a numeric value like in indexed array.


Traversing PHP Associative Array

Traversing an array means to iterate it starting from the first index till the last element of the array.

We can traverse an associative array either using a for loop or foreach. To know the syntax and basic usage of for and foreach loop, you can refer to the PHP for and foreach loop tutorial.


Using for loop

While using the for loop to traverse an associative array we must know the size/length of the array, which can be found using the count() function.

Also, as the index in associative array is not numeric and not sequentially, hence to find the index values or keys(as data saved in associative array is in the form of key-value), we can use the function array_keys() to get an array of the keys used in the associative array.

Following is the syntax for traversing an array using the for loop.

<?php

$lamborghinis = array("Urus", "Huracan", "Aventador");

// find size of the array
$size = count($lamborghinis);

// getting the array of keys/index strings
$keys = array_keys($lamborghinis);

// using the for loop
for($i = 0; $i < $size; $i++)
{
    echo $lamborghinis[$keys[$i]] ." is a/an ". $keys[$i] ." car \n";
}

?>

Urus is a/an suv car Huracan is a/an sports car Aventador is a/an coupe car


Using the foreach loop

using foreach to traverse an associative array is a better approach if you have to traverse the whole array, as we do not have to bother about calculating the size of the array to loop around the array.

Below we have used the foreach to traverse the $lamborghinis array.

<?php

$lamborghinis = array("Urus", "Huracan", "Aventador");

// using the foreach loop
foreach($lamborghinis as $key => $value)
{
    echo $value ."is a/an ". $key ." car \n";
}

?>

Urus is a/an suv car Huracan is a/an sports car Aventador is a/an coupe car

Don't get confused by the syntax as $key=>$value, it means in every iteration of the foreach we are representing the array as key-value pair.


Advantages of Associative Array

Here are a few advantages of using associative array in our program/script:

  1. We can provide more meaningful key or index values to our array elements.
  2. We can save more data, as we can have a string as key to the array element, where we can have associated data to the value to be stored, like in our example, we stored the type of the car as key along with the name of the car as value. Hence storing two different information in one array element.