Associative array creation in PHP - Time & Space Complexity
When we create an associative array in PHP, we want to know how the time it takes grows as we add more items.
We ask: How does adding more key-value pairs affect the work done by the program?
Analyze the time complexity of the following code snippet.
$assocArray = [];
for ($i = 0; $i < $n; $i++) {
$assocArray["key" . $i] = $i;
}
This code creates an associative array by adding n key-value pairs, where keys are strings and values are numbers.
- Primary operation: Adding one key-value pair to the associative array.
- How many times: Exactly
ntimes, once per loop iteration.
Each new item is added one after another, so the total work grows steadily as we add more items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 insertions |
| 100 | About 100 insertions |
| 1000 | About 1000 insertions |
Pattern observation: The work grows directly in proportion to the number of items added.
Time Complexity: O(n)
This means the time to create the associative array grows linearly with the number of key-value pairs added.
[X] Wrong: "Adding items to an associative array is instant and does not depend on the number of items."
[OK] Correct: Each insertion takes some time, so more items mean more total work, even if each insertion is fast.
Understanding how adding items to an associative array scales helps you explain efficiency in real coding tasks and shows you know how data structures behave.
"What if we used numeric keys instead of string keys? How would the time complexity change?"
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
