The PHP associative array are lists or groups that already have multiple values, grouped by indexes or serials.
The basic array syntax would be like the below.
array( ... ); // or [ ... ]
PHP Associative Array Overview
The associative array contains a list of values grouped by labels. So each value inside the associative array has a label name.

The associative array can be written as the below.
<?php
$package = array(
"book" => "Rich Dad",
"softs" => "Anti Virus",
"food" => "Cheese",
"tools" => "Pencil",
"pc" => "Laptop",
"paper" => "Printed Paper"
);
?>
These labels also named as keys.
Anyway, in the following sections I will show you how to create, add, delete elements from the associative array.
Also, you can access all elements by calling it by the square brackets [] for example.
<?php
echo $package["key1"]; // value 1
?>
PHP Associative Array: Creating Elements
To create associative array, you have two ways as the below.
<?php
$package = array(
"user" => "Peter",
"email" => "[email protected]
);
?>
And you can add more elements using the square brackets [] as the below.
<?php
$package["age"] = "35 years";
$package["city"] = "Liverpool";
?>
So each new element has a new different key, Also you can update the array using square brackets [] by calling the same key name of the array element. For example.
<?php
$package["user"] = "Montasser";
?>
In the following section you will learn how to delete an element from the associative array.
Delete an Element From the Associative Array
As I mentioned, the associative array has a key name for each element. So you can delete an element using the key name.
Here I will use unset() PHP built-in function to remove an array element.
<?php
// delete the email element from the array
unset( $array["email"] ); // will delete an element
?>
To loop on the associative array you have to read this article.
Wrapping Up
The associative array contains a list of values grouped by labels. So each value inside the associative array has a label name or key name.
You can access the associative array by calling it by its key name like this: $array["email"]
. Also, to delete an element from the associative array you have to use unset().