0
0
PHPprogramming~20 mins

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

Choose your learning style9 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.