PHP Array Functions Quiz

Quiz 11 15 questions

Prerequisites for the quiz

  1. PHP Array Functions

Are you ready?

15 questions to solve

Instructions
  1. This quiz goes to full-screen once you press the Start button.
  2. At the end of the quiz, you are able to review all the questions that you answered wrong and see their explanations.
What is array_shift() used for?
array_shift() is used to remove the first element from an array and then shift every subsequent element down by one position. Hence, the correct choice is (A). For more info, refer to PHP Array Functions — array_shift().
What is array_unshift() used for?
array_unshift() is used to add a new element at the start of an array. Hence, the correct choice is (B). For more info, refer to PHP Array Functions — array_unshift().

Consider the following code to add a new element 10 at the start of $arr:

<?php

$arr = [1, 2, 3];
$arr = array_unshift($arr, 10);
Identify the issue in the code.
array_unshift() modifies an array in-place and returns its new length. In the code above, we are assigning array_unshift()'s return value back to $arr, which is undesirable for the task at hand. Hence, the correct choice is (C). For more info, refer to PHP Array Functions — array_unshift().
What does the following code output?
<?php

$arr = [1, 2, 3];
array_splice($arr, 1, 1, [5]);

print_r($arr[1]);
array_splice($arr, 1, 1, [5]) removes the second element of $arr and replaces it with 5. Thereafter, $arr[1] returns 5 and that's what the code outputs. This goes with the choice (C). For more info, refer to PHP Array Functions — array_splice().
What does array_slice($arr, 0) return, supposing that $arr is as follows:
<?php

$arr = [0, 10];
array_slice($arr, 0) slices $arr from index 0 to its very end, returning a copy of $arr, that is, the array [0, 10]. Hence, the correct choice is (C). For more info, refer to PHP Array Functions — array_slice().
What does the following code output?
<?php

$arr = [1, 3, 5, 7, 9];
$arr2 = array_slice($arr, 2, 2, true);

print_r($arr2);
array_slice($arr, 2, 2, true) slices $arr from index 2 onwards for 2 elements, while preserving the keys of those elements. This gives the array [ 2 => 5, 3 => 7 ]. Hence, the correct choice is (D). For more info, refer to PHP Array Functions — array_slice().
What does array_splice($arr, 0, 1, 100) return, supposing that $arr is as follows?
<?php

$arr = [-5, 10];
. For more info, refer to PHP Array Functions — array_splice().
What does the following code output?
<?php

$arr = [1, 5];
array_reverse($arr);

echo $arr[0];
array_reverse() takes an array and returns a new array which is the reverse version of the original array. It's important to note that array_reverse() does NOT mutate the original array.

In the code above, we merely call the function without assigning its result back to $arr. So $arr is the same as [1, 5] and, likewise, $arr[0] yields 1. This goes with choice (A). For more info, refer to PHP Array Functions — array_reverse().
What does the following code output?
<?php

$arr = [1, 5];
$arr = array_reverse($arr);

echo $arr[0];
array_reverse() takes an array and returns a new array which is the reverse version of the original array. It's important to note that array_reverse() does NOT mutate the original array.

In the code above, we call the function and then assign the result back to $arr. So $arr becomes [5, 1] and, likewise, $arr[0] yields 5. This goes with choice (B). For more info, refer to PHP Array Functions — array_reverse().
What does the following code output?
<?php

function f($x) { 2 * $x - 1; }

$arr = [1, 2, 10];
array_map('f', $arr);

print_r($arr);
array_map() maps an array to a new array based on a given callback function and returns this new array. Since, in the code above, we don't assign array_map() back to $arr, $arr remains the same as before, i.e. [1, 2, 10]. Hence, the output matches the one shown in choice (B). For more info, refer to PHP Array Functions — array_map().
What does the following code output?
<?php

function f($x) { 2 * $x - 1; }
$arr = array_map('f', [1, 2, 10]);

print_r($arr);
array_map() maps each item of an aray to a new item (in a new array) based on the return value of a given callback function. Since, in the code above, our callback function f() doesn't return anything, each item gets mapped to NULL. Hence, the output matches the one shown in choice (A). For more info, refer to PHP Array Functions — array_map().
What is the array_reduce() function used for?
array_reduce() is used to process all the elements of an array and reduce them down to a single value. This aligns with choice (B). For more info, refer to PHP Array Functions — array_reduce().
How to create an array with 100 elements, each set to the value true?
array_fill() is used to create a new array based on a given value repeated a given number of times. This goes with choice (C). For more info, refer to PHP Array Functions — array_fill().
What is the value of $v in the code below?
<?php

$v = implode('', [1, 2, 3]);
implode() is used to join the elements of an array with a given delimiter and return the result as a string. In the code above, implode('', [1, 2, 3]) joins the elements of [1, 2, 3] with the delimiter '' to ultimately yield the string '123'. Hence, the correct choice is (A). For more info, refer to PHP Array Functions — implode().
What is the value of $v in the code below?
<?php

$v = implode([1, 2, 3]);
The implode() function requires two arguments, however, in the code above, we only provided it with one argument. Likewise, the code throws an error, and this goes with choice (D). For more info, refer to PHP Array Functions — implode().

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage