Array merge and combine in PHP - Time & Space Complexity
When we merge or combine arrays in PHP, the time it takes depends on how many items are involved.
We want to understand how the work grows as the arrays get bigger.
Analyze the time complexity of the following code snippet.
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$result = array_merge($array1, $array2);
$combined = array_combine($array1, $array2);
This code merges two arrays into one and then combines them to create a key-value pair array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing each array to copy or pair elements.
- How many times: Each element in both arrays is visited once during merge and once during combine.
As the size of the arrays grows, the work grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (10 for merge + 10 for combine) |
| 100 | About 200 operations |
| 1000 | About 2000 operations |
Pattern observation: The operations increase linearly as the arrays get bigger.
Time Complexity: O(n)
This means the time to merge and combine grows in a straight line with the number of elements.
[X] Wrong: "Merging two arrays takes constant time no matter their size."
[OK] Correct: The function must look at every element to copy or pair it, so bigger arrays take more time.
Understanding how array operations scale helps you explain your code choices clearly and confidently in interviews.
"What if we merged three arrays instead of two? How would the time complexity change?"
Practice
array_merge do?Solution
Step 1: Understand the purpose of array_merge
The functionarray_mergetakes multiple arrays and joins their elements into a single array.Step 2: Compare with other functions
array_combinecreates key-value pairs, sorting and removing duplicates are different functions.Final Answer:
Joins two or more arrays into one combined array -> Option BQuick Check:
array_merge joins arrays = B [OK]
- Confusing array_merge with array_combine
- Thinking array_merge sorts arrays
- Assuming array_merge removes duplicates
array_combine?Solution
Step 1: Recall array_combine parameter order
The first parameter is the array of keys, the second is the array of values.Step 2: Check each option
array_combine($keys, $values); correctly usesarray_combine($keys, $values);. array_combine($values, $keys); reverses parameters, C uses wrong function, D misses second parameter.Final Answer:
array_combine($keys, $values); -> Option CQuick Check:
Keys first, values second in array_combine = A [OK]
- Swapping keys and values parameters
- Using array_merge instead of array_combine
- Missing one of the required parameters
$a = [1, 2]; $b = [3, 4]; $result = array_merge($a, $b); print_r($result);
Solution
Step 1: Understand array_merge behavior
array_merge joins arrays by appending elements of second array to first, preserving order.Step 2: Apply to given arrays
$a = [1, 2], $b = [3, 4], merged result is [1, 2, 3, 4].Final Answer:
[1, 2, 3, 4] -> Option AQuick Check:
array_merge appends arrays = [1, 2, 3, 4] [OK]
- Thinking array_merge sorts or combines keys
- Confusing output with associative array
- Expecting keys to be preserved as in original arrays
$keys = ['a', 'b']; $values = [1]; $result = array_combine($keys, $values); print_r($result);
Solution
Step 1: Check array_combine requirements
array_combine requires both arrays to have equal length to pair keys and values correctly.Step 2: Analyze given arrays
$keys has 2 elements, $values has 1 element, causing an error.Final Answer:
array_combine requires both arrays to have the same number of elements -> Option DQuick Check:
Equal length arrays needed for array_combine = D [OK]
- Ignoring array length mismatch
- Thinking array_combine works with one array
- Confusing array_combine with array_merge
$keys = ['name', 'age', 'city'];$values = ['Alice', 30, 'Paris'];Which PHP code correctly creates an associative array and then merges it with
['country' => 'France']?Solution
Step 1: Create associative array from keys and values
Usearray_combine($keys, $values)to pair keys with values correctly.Step 2: Merge associative array with another array
Usearray_mergeto add the 'country' key-value pair to the associative array.Step 3: Check other options for errors
$assoc = array_merge($keys, $values); $final = array_combine($assoc, ['country' => 'France']); swaps functions incorrectly, C uses wrong parameter count, D swaps keys and values.Final Answer:
$assoc = array_combine($keys, $values); $final = array_merge($assoc, ['country' => 'France']); -> Option AQuick Check:
Combine keys-values, then merge extra array = A [OK]
- Swapping keys and values in array_combine
- Trying to combine more than two arrays at once
- Using array_merge before array_combine
Ready to prove it?
Pass the test and this pattern moves unknown
