What is an associative array in PHP?

In this article We will discuss in detail about associative array in PHP. In this article We will first discuss array and different types of array in PHP. While discussing different types of arrays, we will discuss the basics of associative arrays. And then We will discuss different aspects of associative arrays in more detail.

What is array in PHP?

Array is a built-in data type of PHP. An array stores a number of values in one single variable. The index in an array indicates the position of the element. We can attach a name to the index. Array with the named key (index) is an associative array. We can access the values of an array by referring to the index number or name.

Different types of PHP Array

There are 3 types of arrays in PHP and those are Indexed arrays, Associative arrays, and Multidimensional arrays.

Indexed array

In indexed array each item of an array has a numeric index.

Create an index array

An example of index array is given below. The var_dump function is used to output the information of the array.

<?php
$colors = ['blue', 'green', 'red'];
var_dump($colors);
?>

The output is:

array(3) {
  [0]=>
  string(4) "blue"
  [1]=>
  string(5) "green"
  [2]=>
  string(3) "red"
}

Here we have not named any index while creating the array. Here the index ie 0,1,2 indicates the position of the array items.

Associative Array

In associative array each item of an array has named keys. This key is named by us while creating or modifying the array.

Create associative array

An example of associative array is given below. We have used the var_dump function here also to dump the array information.

<?php
$shape = ["shape_type"=>"circle", "color"=>"blue"];
var_dump($shape);
?>

Here is the output of the above code:

array(2) {
  ["shape_type"]=>
  string(6) "circle"
  ["color"]=>
  string(4) "blue"
}

Here we name the index of each item while creating it.

Multidimensional Arrays

A multidimensional array can store one or more than one arrays.

Create Multidimensional array

An example is of Multidimensional array given below.

<?php
$shapes = [
	    ["shape_type"=>"circle", "color"=>"blue"],
            ["shape_type"=>"rectangle", "color"=>"green"],
            ["shape_type"=>"square", "color"=>"red"]
          ];
var_dump($shapes);
?>

The output of the above code is:

array(3) {
  [0]=>
  array(2) {
    ["shape_type"]=>
    string(6) "circle"
    ["color"]=>
    string(4) "blue"
  }
  [1]=>
  array(2) {
    ["shape_type"]=>
    string(9) "rectangle"
    ["color"]=>
    string(5) "green"
  }
  [2]=>
  array(2) {
    ["shape_type"]=>
    string(6) "square"
    ["color"]=>
    string(3) "red"
  }
}

From the above example we see that this array is storing 3 arrays and whose indexes are 0,1,2 respectively.

More on Associative Array

Here we will discuss in more details how Associate Array works in PHP.

Access array item of an Associative Array

We can access an array item by referring the key name to the associative array. An example of this is given below.

<?php
$shape = ["shape_type"=>"circle", "color"=>"blue"];
echo $shape["shape_type"];
echo "<br>";
echo $shape["color"];
?>

The above example will output:

circle
blue

From the above example we can see that we access circle by rereferring the shape_type key name in the array and blue by rereferring the color key name in the array.

Modify array item of an Associative Array

We can modify the value of an array item in an associative array by assigning a new value after accessing it (by referring the key name to the array).

For the example, we will use the array of the above example. Here we will change the item value of the color key of the array from blue to green. For that, we will access the value by referring the key name first and then we will assign the value to green.

<?php
$shape = ["shape_type"=>"circle", "color"=>"blue"];
$shape["color"] = "green";
var_dump($shape);
?>

Here the output of the above code is:

array(2) {
  ["shape_type"]=>
  string(6) "circle"
  ["color"]=>
  string(5) "green"
}

We can see from the output that the item value of the color key is changed from blue to green.

Remove array item of an Associative Array

We can remove an array item in an associative array by using unset function after accessing the array item. For the example, we will use the same array of the above example. Here we remove the item of the color key of the array. The example is as follows:

<?php
$shape = ["shape_type"=>"circle", "color"=>"blue"];
unset($shape["color"]);
var_dump($shape);
?>

The output of the above example is:

array(1) {
  ["shape_type"]=>
  string(6) "circle"
}

From the above output we can clearly see that the item of the color key index has been removed from the array.

Traversing an Associative Array

To traverse an associative arrays we use loop. In the example below, We will loop through all the items of an associative array and print all keys and values of array items.

<?php
$shape = ["shape_type"=>"circle", "color"=>"blue"];
foreach ($shape as $key => $value) {
  echo "$key = $value <br>";
}
?>

The output of the above code is:

shape_type = circle
color = blue

In this example, we have traversed the associative arrays and displayed all keys and values of every array items.

Advantage of using associative array

  1. Indexed array has only numeric index but associative array uses named index so it is more practical, more readable and more flexible to manage the code.
  2. Accessing, adding, modifying and removing array items in an associative array is easy because of the named index.
  3. Searching and accessing values in associative arrays in PHP is efficient because internally it uses hash tables.
  4. Associative array is very useful for managing data that has key-value relationship, such as the site setting or configuration of an application.

2 thoughts on “What is an associative array in PHP?”

  1. Pingback: Webtechbased
  2. Pingback: Webtechbased

Comments are closed.