An associative array lets you store data with named keys instead of numbers. It helps you find values by names, like a small dictionary.
Associative array creation in PHP
<?php
$associativeArray = [
"key1" => "value1",
"key2" => "value2",
"key3" => "value3"
];
?>Keys are usually strings but can also be integers.
Use the => symbol to assign a value to a key.
<?php
$person = [
"name" => "Alice",
"age" => 30,
"city" => "Paris"
];
?><?php $emptyArray = []; ?>
<?php $singleItem = ["color" => "blue"]; ?>
<?php $numbers = [1 => "one", 2 => "two", 3 => "three"]; ?>
This program creates an associative array for a car with make, model, and year. It prints the array, adds a new key 'color', then prints the updated array.
<?php // Create an associative array with some data $car = [ "make" => "Toyota", "model" => "Corolla", "year" => 2020 ]; // Print the array before adding a new item print_r($car); // Add a new key-value pair $car["color"] = "red"; // Print the array after adding the new item print_r($car); ?>
Accessing a value by key is very fast, usually O(1) time.
Associative arrays use more memory than simple indexed arrays because they store keys.
Common mistake: forgetting quotes around string keys can cause errors.
Use associative arrays when you want to label data clearly; use indexed arrays when order matters more than names.
Associative arrays store data with named keys for easy access.
Use the => symbol to assign values to keys inside square brackets.
They are great for grouping related data with meaningful names.
Practice
"name" and "age"?Solution
Step 1: Understand PHP associative array syntax
Associative arrays use square brackets with key => value pairs.Step 2: Identify correct syntax among options
$person = ["name" => "Alice", "age" => 30]; uses square brackets and => correctly; others use invalid syntax.Final Answer:
$person = ["name" => "Alice", "age" => 30]; -> Option DQuick Check:
Associative array = key => value pairs [OK]
- Using parentheses instead of square brackets
- Using colon (:) instead of =>
- Creating indexed arrays instead of associative
Solution
Step 1: Check the syntax for key-value pairs
PHP uses => to assign values to keys, not colon (:).Step 2: Identify the invalid syntax
$data = ["key1" : "value1", "key2" : "value2"]; uses colon (:) which is invalid in PHP associative arrays.Final Answer:
$data = ["key1" : "value1", "key2" : "value2"]; -> Option AQuick Check:
Use => not : for keys in PHP arrays [OK]
- Using colon (:) instead of =>
- Mixing array() and [] syntax incorrectly
- Missing commas between pairs
$arr = ["fruit" => "apple", "color" => "red"]; echo $arr["color"];
Solution
Step 1: Understand array key access
Accessing $arr["color"] returns the value for key "color".Step 2: Identify the value for key "color"
Value is "red", so echo outputs "red".Final Answer:
red -> Option AQuick Check:
Key "color" = "red" [OK]
- Confusing keys and values
- Using wrong quotes or missing quotes
- Trying to access with dot notation
$info = ["name" => "John", "age" = 25];
Solution
Step 1: Check key-value assignment syntax
PHP requires => to assign values to keys, not =.Step 2: Locate incorrect assignment
"age" = 25 uses = instead of =>, causing syntax error.Final Answer:
Using = instead of => for key-value pair -> Option CQuick Check:
Use => for key-value pairs [OK]
- Using single = instead of =>
- Omitting commas between pairs
- Not quoting string keys
Solution
Step 1: Understand foreach key-value syntax
Use foreach ($array as $key => $value) to access keys and values correctly.Step 2: Filter products with price > 20
$products = ["apple" => 15, "banana" => 25, "cherry" => 30]; $filtered = []; foreach ($products as $name => $price) { if ($price > 20) { $filtered[$name] = $price; } } correctly checks price and assigns filtered items to new array.Final Answer:
The code using foreach ($products as $name => $price) -> Option BQuick Check:
foreach ($arr as $key => $value) for associative arrays [OK]
- Swapping key and value variables in foreach
- Using incorrect foreach syntax with two variables separated by comma
- Misusing array_filter with wrong callback parameters
Ready to prove it?
Pass the test and this pattern moves unknown
