← Back to PHP Course | Chapter 7: Arrays | Lesson 8 of 13

PHP Associative Arrays

An associative array is like a dictionary where you look up a word to find its meaning. Instead of numbers, you use names such as age or city to find each value.
Syntax
php
$array = ["key1" => value1, "key2" => value2];

$array["key1"];             // read
$array["key3"] = value3;   // add

foreach ($array as $key => $value) { }

What are Associative Arrays?

An associative array uses strings (or any scalar value) as keys instead of automatic integers, letting you store data as labeled fields — like $user[email] — which reads far more clearly than a numbered index would.

Example: What are Associative Arrays?

php
<?php
// Declare `$user` as an array: `['email' => '[email protected]']`
$user = ['email' => '[email protected]'];
// Print `$user['email']` to the output
echo $user['email'];
?>

Accessing and Modifying Elements

You define one with [name => Aditi, age => 24], where => separates each key from its value, and this is the closest PHP equivalent to what other languages call a dictionary, hash map, or object literal.

Example: Accessing and Modifying Elements

php
<?php
// Declare `$person` as an array: `['name' => 'Aditi', 'age' => 24]`
$person = ['name' => 'Aditi', 'age' => 24];
// Print `$person['name'] . " is " . $person['age']` to the output
echo $person['name'] . " is " . $person['age'];
?>

Adding New Key-Value Pairs

foreach ($array as $key => $value) lets you loop over both the label and the data at once, which is the standard way to print out or process every field in an associative array such as a user's profile.

Example: Adding New Key-Value Pairs

php
<?php
// Declare `$profile` as an array: `['name' => 'Alice', 'age' => 30]`
$profile = ['name' => 'Alice', 'age' => 30];
// Loop over `$profile`, binding each item to `$key => $value`
foreach ($profile as $key => $value) {
    // Print "$key: $value\n" to the output
    echo "$key: $value\n";
}
?>

Looping Through Associative Arrays

isset($array[key]) checks whether a given key exists before you try to read it, protecting against PHP notices when optional fields — like a middle name — might be missing from the array.

Example: Looping Through Associative Arrays

php
<?php
// Declare `$person` as an array: `['name' => 'Alice']`
$person = ['name' => 'Alice'];
// Check whether `isset($person['middleName'])`
if (isset($person['middleName'])) {
    // Print `$person['middleName']` to the output
    echo $person['middleName'];
// Otherwise, run this branch
} else {
    // Print "No middle name set." to the output
    echo "No middle name set.";
}
?>

Checking If a Key Exists

Associative arrays are the natural fit for representing a single structured record, such as one row from a database query, where each key names a specific column and its value.

Example: Checking If a Key Exists

php
<?php
// Declare `$row` as an array: `['id' => 1, 'name' => 'Alice', 'email' => '[email protected]']`
$row = ['id' => 1, 'name' => 'Alice', 'email' => '[email protected]'];
// Print `$row['name']` to the output
echo $row['name'];
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Reading a key that does not exist, which raises an undefined array key warning; check with isset or ??.
  2. Writing a key without quotes such as $arr[name], which is treated as a constant and fails in PHP 8.
  3. Using duplicate keys in an array literal and expecting both, when the last value overwrites the earlier one.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.