It joins two or more arrays into one array by appending the elements of later arrays to the first array. If keys are numeric, they are re-indexed; if keys are strings, later values overwrite earlier ones.
Click to reveal answer
beginner
How does array_combine() work in PHP?
It creates a new array by using one array for keys and another array for values. Both arrays must have the same number of elements.
Click to reveal answer
intermediate
What happens if the arrays passed to array_combine() have different lengths?
PHP will return FALSE because the function requires both arrays to have the same number of elements to pair keys and values correctly.
Click to reveal answer
beginner
Given two arrays $a = [1, 2] and $b = [3, 4], what is the result of array_merge($a, $b)?
The result is [1, 2, 3, 4]. The elements of $b are appended to $a and re-indexed numerically.
Click to reveal answer
beginner
How can you combine keys and values arrays into an associative array in PHP?
Use array_combine($keys, $values) where $keys is an array of keys and $values is an array of values. The result is an associative array pairing each key with its value.
Click to reveal answer
What does array_merge() do when merging arrays with string keys?
ALater values overwrite earlier ones for the same keys
BIt appends values and keeps all keys duplicated
CIt ignores string keys and only merges numeric keys
DIt throws an error
✗ Incorrect
When merging arrays with string keys, array_merge() overwrites earlier values with later ones for the same keys.
What must be true for array_combine() to work correctly?
ABoth arrays must have unique values
BBoth arrays must have numeric keys
CBoth arrays must be empty
DBoth arrays must have the same number of elements
✗ Incorrect
array_combine() requires both arrays to have the same number of elements to pair keys and values.
What will array_merge(["a" => 1], ["a" => 2]) return?
A["a" => 2]
B["a" => 1, "a" => 2]
C[1, 2]
DAn error
✗ Incorrect
The second array's value overwrites the first for the same string key "a".
What does array_combine(["x", "y"], [10, 20]) produce?
A[10, 20]
B["x" => 10, "y" => 20]
C["x", "y", 10, 20]
DFALSE
✗ Incorrect
It creates an associative array with keys "x" and "y" paired to values 10 and 20.
What happens if you pass arrays of different sizes to array_combine()?
AIt fills missing values with NULL
BIt merges arrays ignoring extra elements
CIt returns FALSE
DIt throws a warning but returns partial array
✗ Incorrect
PHP returns FALSE if the arrays have different lengths because it cannot pair keys and values properly.
Explain how array_merge() works when combining two arrays with numeric and string keys.
Think about how PHP handles keys differently based on their type.
You got /3 concepts.
Describe the requirements and result of using array_combine() in PHP.
Consider what happens if the arrays differ in size.
You got /4 concepts.
Practice
(1/5)
1. What does the PHP function array_merge do?
easy
A. Creates an associative array from two arrays
B. Joins two or more arrays into one combined array
C. Sorts an array in ascending order
D. Removes duplicate values from an array
Solution
Step 1: Understand the purpose of array_merge
The function array_merge takes multiple arrays and joins their elements into a single array.
Step 2: Compare with other functions
array_combine creates key-value pairs, sorting and removing duplicates are different functions.
Final Answer:
Joins two or more arrays into one combined array -> Option B
Quick Check:
array_merge joins arrays = B [OK]
Hint: array_merge joins arrays end-to-end [OK]
Common Mistakes:
Confusing array_merge with array_combine
Thinking array_merge sorts arrays
Assuming array_merge removes duplicates
2. Which of the following is the correct syntax to combine two arrays into an associative array using array_combine?
easy
A. array_merge($keys, $values);
B. array_combine($values, $keys);
C. array_combine($keys, $values);
D. array_combine($keys);
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 uses array_combine($keys, $values);. array_combine($values, $keys); reverses parameters, C uses wrong function, D misses second parameter.
Final Answer:
array_combine($keys, $values); -> Option C
Quick Check:
Keys first, values second in array_combine = A [OK]
Hint: Keys go first, values second in array_combine [OK]
A. array_combine merges arrays instead of combining keys and values
B. array_combine cannot use string keys
C. array_combine needs only one parameter
D. array_combine requires both arrays to have the same number of elements
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 D
Quick Check:
Equal length arrays needed for array_combine = D [OK]
Hint: Keys and values arrays must be same length [OK]
Common Mistakes:
Ignoring array length mismatch
Thinking array_combine works with one array
Confusing array_combine with array_merge
5. Given two arrays: $keys = ['name', 'age', 'city']; $values = ['Alice', 30, 'Paris']; Which PHP code correctly creates an associative array and then merges it with ['country' => 'France']?