Array key and value extraction in PHP - Time & Space Complexity
When we extract keys and values from an array, we want to know how the time to do this grows as the array gets bigger.
We ask: How much work does the computer do when it pulls out all keys or values?
Analyze the time complexity of the following code snippet.
$array = ["a" => 1, "b" => 2, "c" => 3];
$keys = array_keys($array);
$values = array_values($array);
This code gets all the keys and all the values from an associative array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the entire array once to collect keys or values.
- How many times: Each function loops through all elements once, so twice total for keys and values.
As the array grows, the work grows in a straight line with the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 (10 for keys + 10 for values) |
| 100 | About 200 (100 for keys + 100 for values) |
| 1000 | About 2000 (1000 for keys + 1000 for values) |
Pattern observation: The operations grow directly with the number of elements, doubling because keys and values are extracted separately.
Time Complexity: O(n)
This means the time to extract keys and values grows in a straight line with the number of items in the array.
[X] Wrong: "Extracting keys and values is instant and does not depend on array size."
[OK] Correct: The computer must look at each element to get keys or values, so more elements mean more work.
Understanding how simple operations like extracting keys and values scale helps you reason about bigger problems and write efficient code.
"What if we extract keys and values together in one loop instead of separately? How would the time complexity change?"
Practice
Solution
Step 1: Understand the function purpose
array_keys()returns all keys from an array.Step 2: Compare with other functions
array_values()returns values, not keys; others do different tasks.Final Answer:
array_keys() -> Option CQuick Check:
Keys extraction = array_keys() [OK]
- Confusing array_keys() with array_values()
- Using array_map() to get keys
- Trying to get keys with array_filter()
$arr in PHP?Solution
Step 1: Identify the correct function name
The function to get values isarray_values().Step 2: Check syntax correctness
Correct syntax isarray_values($arr);with parentheses and variable inside.Final Answer:
array_values($arr); -> Option AQuick Check:
Values extraction = array_values() [OK]
- Using array_keys() to get values
- Using non-existent functions like values_array()
- Missing parentheses in function call
$arr = ['a' => 1, 'b' => 2, 'c' => 3]; print_r(array_keys($arr));
Solution
Step 1: Understand array_keys() output
It returns an indexed array of all keys from the input array.Step 2: Analyze given array keys
Keys are strings 'a', 'b', 'c', so output is an indexed array with these strings.Final Answer:
Array ( [0] => a [1] => b [2] => c ) -> Option DQuick Check:
array_keys(['a'=>1,...]) = ['a','b','c'] [OK]
- Confusing keys with values in output
- Expecting associative array output
- Including quotes in output keys
$arr = ['x' => 10, 'y' => 20]; $keys = array_values($arr); print_r($keys);
Solution
Step 1: Understand the goal
The code tries to get keys but usesarray_values()which returns values.Step 2: Correct function usage
To get keys, usearray_keys()instead ofarray_values().Final Answer:
array_values() should be array_keys() to get keys -> Option AQuick Check:
Keys need array_keys(), not array_values() [OK]
- Mixing up array_keys() and array_values()
- Assuming print_r() can't print arrays
- Ignoring correct variable definitions
$data = ['apple' => 3, 'banana' => 0, 'cherry' => 5, 'date' => 0];, which PHP code correctly extracts only the keys with non-zero values?Solution
Step 1: Filter array to keep non-zero values
array_filter($data)removes entries with zero values.Step 2: Extract keys from filtered array
array_keys()gets keys from the filtered array, giving only keys with non-zero values.Final Answer:
$keys = array_keys(array_filter($data)); -> Option BQuick Check:
Filter then keys = array_keys(array_filter()) [OK]
- Using array_values() instead of array_keys() after filtering
- Not filtering before extracting keys
- Filtering keys instead of values
Ready to prove it?
Pass the test and this pattern moves unknown
