0
0
PHPprogramming~10 mins

Array merge and combine in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to merge two arrays using the correct function.

PHP
<?php
$array1 = [1, 2];
$array2 = [3, 4];
$result = [1]($array1, $array2);
print_r($result);
?>
Drag options to blanks, or click blank then click option'
Aarray_slice
Barray_combine
Carray_push
Darray_merge
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_combine instead of array_merge
Using array_push which adds elements to an array but does not merge arrays
2fill in blank
medium

Complete the code to combine two arrays into a key-value array.

PHP
<?php
$keys = ['a', 'b'];
$values = [1, 2];
$combined = [1]($keys, $values);
print_r($combined);
?>
Drag options to blanks, or click blank then click option'
Aarray_flip
Barray_combine
Carray_slice
Darray_merge
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_merge which just joins arrays
Using array_flip which swaps keys and values
3fill in blank
hard

Fix the error in the code to correctly merge arrays with numeric keys.

PHP
<?php
$array1 = [0 => 'apple', 1 => 'banana'];
$array2 = [0 => 'cherry', 1 => 'date'];
$merged = [1]($array1, $array2);
print_r($merged);
?>
Drag options to blanks, or click blank then click option'
Aarray_combine
Barray_replace
Carray_merge
Darray_push
Attempts:
3 left
💡 Hint
Common Mistakes
Using array_combine which requires equal length and uses one array as keys
Using array_replace which overwrites values instead of merging
4fill in blank
hard

Fill both blanks to create an array of squares for even numbers only.

PHP
<?php
$numbers = [1, 2, 3, 4, 5];
$squares = [
    [1] => [2]
    for [1] in $numbers if [1] % 2 == 0
];
print_r($squares);
?>
Drag options to blanks, or click blank then click option'
A$num
B$num * $num
C$number
D$number * $number
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the loop and expression
Not squaring the number correctly
5fill in blank
hard

Fill all three blanks to create an associative array with uppercase keys and values greater than 10.

PHP
<?php
$data = ['a' => 5, 'b' => 15, 'c' => 20];
$result = [
    [1] => [2]
    for [1], [2] in $data if [3] > 10
];
print_r($result);
?>
Drag options to blanks, or click blank then click option'
A$key
B$value
Dstrtoupper($key)
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable name for key and value
Not converting keys to uppercase