Bird
Raised Fist0
PHPprogramming~20 mins

Array merge and combine in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Array Merge and Combine Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of merging two arrays with array_merge?

Consider the following PHP code that merges two arrays. What will be the output?

PHP
<?php
$array1 = ["a" => "apple", "b" => "banana"];
$array2 = ["a" => "apricot", "c" => "cherry"];
$result = array_merge($array1, $array2);
print_r($result);
?>
AArray ( [a] => apricot [b] => banana [c] => cherry )
BArray ( [a] => apple [b] => banana [c] => cherry )
CArray ( [a] => apple [a] => apricot [b] => banana [c] => cherry )
DArray ( [a] => apple [b] => banana )
Attempts:
2 left
💡 Hint

Remember that array_merge overwrites values with the same string keys from later arrays.

❓ Predict Output
intermediate
2:00remaining
What does array_combine produce with two arrays?

Given two arrays, what will be the result of array_combine?

PHP
<?php
$keys = ["x", "y", "z"];
$values = [10, 20, 30];
$result = array_combine($keys, $values);
print_r($result);
?>
AArray ( [x] => 10 [y] => 20 [z] => 30 )
BArray ( [10] => x [20] => y [30] => z )
CArray ( [0] => x [1] => y [2] => z )
DArray ( [x] => 0 [y] => 1 [z] => 2 )
Attempts:
2 left
💡 Hint

array_combine uses the first array as keys and the second as values.

🔧 Debug
advanced
2:00remaining
What error does this array_combine call produce?

Look at this PHP code snippet. What error will it produce when run?

PHP
<?php
$keys = ["a", "b"];
$values = [1, 2, 3];
$result = array_combine($keys, $values);
print_r($result);
?>
ANo error, output: Array ( [a] => 1 [b] => 2 )
BFatal error: Cannot use array_combine with numeric keys
COutput: Array ( [a] => 1 [b] => 2 [3] => )
DWarning: array_combine(): Both parameters should have equal number of elements
Attempts:
2 left
💡 Hint

Check if both arrays have the same number of elements.

❓ Predict Output
advanced
2:00remaining
What is the output of merging numeric arrays with array_merge?

What will this PHP code output?

PHP
<?php
$array1 = [1, 2, 3];
$array2 = [4, 5];
$result = array_merge($array1, $array2);
print_r($result);
?>
AArray ( [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
BArray ( [0] => 4 [1] => 5 [2] => 1 [3] => 2 [4] => 3 )
CArray ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
DArray ( [0] => 1 [1] => 2 [2] => 3 )
Attempts:
2 left
💡 Hint

For numeric keys, array_merge appends values and reindexes keys.

🧠 Conceptual
expert
2:00remaining
How many items are in the resulting array after merging these arrays?

Given these two arrays, how many items will the resulting array have after merging with array_merge?

PHP
<?php
$array1 = ["x" => 1, "y" => 2, 3];
$array2 = ["y" => 4, 5, 6];
$result = array_merge($array1, $array2);
?>
A6
B5
C4
D7
Attempts:
2 left
💡 Hint

Remember how array_merge handles string keys and numeric keys differently.

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

  1. Step 1: Understand the purpose of array_merge

    The function array_merge takes multiple arrays and joins their elements into a single array.
  2. Step 2: Compare with other functions

    array_combine creates key-value pairs, sorting and removing duplicates are different functions.
  3. Final Answer:

    Joins two or more arrays into one combined array -> Option B
  4. 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

  1. Step 1: Recall array_combine parameter order

    The first parameter is the array of keys, the second is the array of values.
  2. 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.
  3. Final Answer:

    array_combine($keys, $values); -> Option C
  4. Quick Check:

    Keys first, values second in array_combine = A [OK]
Hint: Keys go first, values second in array_combine [OK]
Common Mistakes:
  • Swapping keys and values parameters
  • Using array_merge instead of array_combine
  • Missing one of the required parameters
3. What is the output of the following PHP code?
$a = [1, 2];
$b = [3, 4];
$result = array_merge($a, $b);
print_r($result);
medium
A. [1, 2, 3, 4]
B. [3, 4, 1, 2]
C. [1 => 2, 3 => 4]
D. Array keys and values combined

Solution

  1. Step 1: Understand array_merge behavior

    array_merge joins arrays by appending elements of second array to first, preserving order.
  2. Step 2: Apply to given arrays

    $a = [1, 2], $b = [3, 4], merged result is [1, 2, 3, 4].
  3. Final Answer:

    [1, 2, 3, 4] -> Option A
  4. Quick Check:

    array_merge appends arrays = [1, 2, 3, 4] [OK]
Hint: array_merge appends arrays in order given [OK]
Common Mistakes:
  • Thinking array_merge sorts or combines keys
  • Confusing output with associative array
  • Expecting keys to be preserved as in original arrays
4. Identify the error in this PHP code snippet:
$keys = ['a', 'b'];
$values = [1];
$result = array_combine($keys, $values);
print_r($result);
medium
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

  1. Step 1: Check array_combine requirements

    array_combine requires both arrays to have equal length to pair keys and values correctly.
  2. Step 2: Analyze given arrays

    $keys has 2 elements, $values has 1 element, causing an error.
  3. Final Answer:

    array_combine requires both arrays to have the same number of elements -> Option D
  4. 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']?
hard
A. $assoc = array_combine($keys, $values); $final = array_merge($assoc, ['country' => 'France']);
B. $assoc = array_merge($keys, $values); $final = array_combine($assoc, ['country' => 'France']);
C. $final = array_combine($keys, $values, ['country' => 'France']);
D. $final = array_merge(array_combine($values, $keys), ['country' => 'France']);

Solution

  1. Step 1: Create associative array from keys and values

    Use array_combine($keys, $values) to pair keys with values correctly.
  2. Step 2: Merge associative array with another array

    Use array_merge to add the 'country' key-value pair to the associative array.
  3. 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.
  4. Final Answer:

    $assoc = array_combine($keys, $values); $final = array_merge($assoc, ['country' => 'France']); -> Option A
  5. Quick Check:

    Combine keys-values, then merge extra array = A [OK]
Hint: Combine keys-values first, then merge extra pairs [OK]
Common Mistakes:
  • Swapping keys and values in array_combine
  • Trying to combine more than two arrays at once
  • Using array_merge before array_combine
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard