An associative array in PHP stores values using named keys instead of relying only on numeric indexes. It is useful for organizing related data, such as user details, product information, and configuration settings.

- Named Keys: Associates values with meaningful string keys.
- Flexible Values: Supports different data types within the same array.
- Easy Access: Allows elements to be accessed, modified, and removed using their keys.
Creating an Associative Array in PHP
PHP supports two ways to create associative arrays using the array() function or short array syntax ([]). The => operator associates each key with its value.
1. Using array()
<?php
$person = array(
"name" => "Alen",
"age" => 23,
"city" => "New York"
);
?>
Using short array syntax
<?php
$person = [
"name" => "Alen",
"age" => 23,
"city" => "New York"
];
?>
The short array syntax is generally preferred in modern PHP code.
Accessing Values in an Associative Array
Access an element by specifying its key inside square brackets.
<?php
$person = [
"name" => "Alen",
"age" => 23,
"city" => "New York"
];
echo $person["name"] . "\n";
echo $person["age"] . "\n";
echo $person["city"];
?>
Iterating Through an Associative Array
The foreach loop is the simplest way to iterate through an associative array. It allows you to access both keys and values.
1. Using the foreach Loop
<?php
$fruits = [
"apple" => "red",
"banana" => "yellow",
"grape" => "purple"
];
foreach ($fruits as $fruit => $color) {
echo "The color of $fruit is $color.\n";
}
?>
Here, $fruit stores each key, while $color stores its corresponding value.
2. Using a for Loop
A for loop can iterate through an associative array by first retrieving its keys using array_keys().
<?php
$fruits = [
"apple" => "red",
"banana" => "yellow",
"grape" => "purple"
];
$keys = array_keys($fruits);
$total = count($keys);
for ($i = 0; $i < $total; $i++) {
$fruit = $keys[$i];
echo "The color of $fruit is " . $fruits[$fruit] . ".\n";
}
?>
For most associative array operations, foreach is more readable and requires less code than a for loop.
Checking Whether a Key Exists
PHP provides array_key_exists() and isset() to check whether an array key exists. The main difference is that array_key_exists() returns true even when the key's value is null, whereas isset() returns false for a key with a null value.
<?php
$data = [
"x" => 100,
"y" => 200,
"z" => null
];
if (array_key_exists("x", $data)) {
echo "Key 'x' exists.\n";
}
if (isset($data["z"])) {
echo "Key 'z' is set.";
} else {
echo "Key 'z' is missing or has a null value.";
}
?>
Use array_key_exists() when you need to distinguish between a missing key and a key whose value is null.
Modifying and Removing Elements
You can update an existing value by assigning a new value to its key. Use unset() to remove a key-value pair.
1. Modifying an Element
<?php
$person = [
"name" => "Alen",
"age" => 23
];
$person["age"] = 24;
echo $person["age"];
?>
2. Removing an Element
<?php
$items = [
"first" => "one",
"second" => "two",
"third" => "three"
];
unset($items["second"]);
print_r($items);
?>
Useful Built-in Functions for Associative Arrays
PHP provides several functions for working with associative arrays.
- array_keys($array): Returns all keys in an array.
- array_values($array): Returns all values in an array.
- array_key_exists("key", $array): Checks whether a key exists, even if its value is null.
- count($array): Returns the number of elements in an array.
- unset($array["key"]): Removes an element by its key.