PHP Associative Arrays
In this page:
$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
// Declare `$user` as an array: `['email' => '[email protected]']`
$user = ['email' => '[email protected]'];
// Print `$user['email']` to the output
echo $user['email'];
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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'];
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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.";
}
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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'];
?>
Login to try C/C++/Java/PHP code in the editor
- Reading a key that does not exist, which raises an undefined array key warning; check with
issetor??. - Writing a key without quotes such as
$arr[name], which is treated as a constant and fails in PHP 8. - Using duplicate keys in an array literal and expecting both, when the last value overwrites the earlier one.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: