0
0
PHPprogramming~20 mins

Array slice and splice in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Slice and Splice 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 this PHP array_slice example?
Consider the following PHP code. What will be the output when it runs?
PHP
<?php
$array = [10, 20, 30, 40, 50];
$result = array_slice($array, 2, 2);
print_r($result);
?>
AArray\n(\n [0] => 30\n [1] => 40\n)
BArray\n(\n [2] => 30\n [3] => 40\n)
CArray\n(\n [0] => 20\n [1] => 30\n)
DArray\n(\n [0] => 40\n [1] => 50\n)
Attempts:
2 left
💡 Hint
array_slice reindexes keys by default unless you specify the fourth parameter as true.
Predict Output
intermediate
2:00remaining
What does this PHP array_splice code output?
Look at this PHP code snippet. What will be printed?
PHP
<?php
$array = [1, 2, 3, 4, 5];
$removed = array_splice($array, 1, 3, [9, 8]);
print_r($array);
print_r($removed);
?>
AArray\n(\n [0] => 1\n [1] => 9\n [2] => 8\n [3] => 5\n)\nArray\n(\n [0] => 2\n [1] => 3\n [2] => 4\n)
BArray\n(\n [0] => 1\n [1] => 2\n [2] => 3\n [3] => 4\n [4] => 5\n [5] => 9\n [6] => 8\n)\nArray\n()
CArray\n(\n [0] => 9\n [1] => 8\n)\nArray\n(\n [0] => 1\n [1] => 2\n [2] => 3\n [3] => 4\n [4] => 5\n)
DArray\n(\n [0] => 1\n [1] => 9\n [2] => 8\n)\nArray\n(\n [0] => 2\n [1] => 3\n)
Attempts:
2 left
💡 Hint
array_splice removes elements and replaces them with new ones, returning the removed elements.
🔧 Debug
advanced
2:00remaining
What error does this PHP code raise?
Examine this PHP snippet. What error will it produce when run?
PHP
<?php
$array = [5, 10, 15];
$result = array_slice($array, -1, -1);
print_r($result);
?>
AArray\n() (an empty array)
BWarning: Length parameter cannot be negative in array_slice()
CArray\n(\n [0] => 15\n)
DFatal error: Uncaught Exception
Attempts:
2 left
💡 Hint
Check the meaning of negative length in array_slice.
Predict Output
advanced
2:00remaining
What is the output of this PHP array_splice with no length parameter?
What will this PHP code print?
PHP
<?php
$array = [100, 200, 300, 400];
$removed = array_splice($array, 2);
print_r($array);
print_r($removed);
?>
AArray\n(\n [0] => 300\n [1] => 400\n)\nArray\n(\n [0] => 100\n [1] => 200\n)
BArray\n(\n [0] => 100\n [1] => 200\n [2] => 300\n [3] => 400\n)\nArray\n()
CArray\n(\n [0] => 100\n [1] => 200\n)\nArray\n(\n [0] => 300\n [1] => 400\n)
DArray\n()\nArray\n(\n [0] => 100\n [1] => 200\n [2] => 300\n [3] => 400\n)
Attempts:
2 left
💡 Hint
If length is omitted, array_splice removes all elements from offset to end.
🧠 Conceptual
expert
2:00remaining
How many elements remain in the array after this PHP array_splice operation?
Given this PHP code, how many elements will remain in $array after the operation?
PHP
<?php
$array = [7, 14, 21, 28, 35, 42];
array_splice($array, 2, 2, [99, 100, 101]);
?>
A5
B4
C6
D7
Attempts:
2 left
💡 Hint
Count how many elements are removed and how many are inserted.