Bird
Raised Fist0
PHPprogramming~10 mins

Array slice and splice in PHP - Interactive Code Practice

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

Complete the code to get a slice of the array from index 1 to 3.

PHP
<?php
$fruits = ['apple', 'banana', 'cherry', 'date', 'fig'];
$slice = array_slice($fruits, [1], 3);
print_r($slice);
?>
Drag options to blanks, or click blank then click option'
A1
B0
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as the start index which includes the first element.
Using 3 which starts slicing too late.
2fill in blank
medium

Complete the code to remove 2 elements from the array starting at index 2 using array_splice.

PHP
<?php
$colors = ['red', 'green', 'blue', 'yellow', 'purple'];
array_splice($colors, [1], 2);
print_r($colors);
?>
Drag options to blanks, or click blank then click option'
A0
B1
C3
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 which removes elements starting too early.
Using 3 which removes elements starting too late.
3fill in blank
hard

Fix the error in the code to correctly insert 'orange' at index 1 without removing elements.

PHP
<?php
$fruits = ['apple', 'banana', 'cherry'];
array_splice($fruits, [1], 0, 'orange');
print_r($fruits);
?>
Drag options to blanks, or click blank then click option'
A0
B2
C1
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 inserts before 'apple'.
Using 2 inserts after 'banana'.
4fill in blank
hard

Fill both blanks to create a slice of the array from index 2 to the end.

PHP
<?php
$numbers = [10, 20, 30, 40, 50];
$slice = array_slice($numbers, [1], [2]);
print_r($slice);
?>
Drag options to blanks, or click blank then click option'
A2
B3
Cnull
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as start index returns the first three elements.
Using 0 as length returns an empty array.
5fill in blank
hard

Fill all three blanks to remove 1 element at index 1 and insert 'kiwi' and 'mango' instead.

PHP
<?php
$fruits = ['apple', 'banana', 'cherry', 'date'];
array_splice($fruits, [1], [2], [[3]]);
print_r($fruits);
?>
Drag options to blanks, or click blank then click option'
A1
B2
C'kiwi', 'mango'
Attempts:
3 left
💡 Hint
Common Mistakes
Removing 2 elements removes too many items.
Not passing the inserted fruits as an array.

Practice

(1/5)
1. What is the main difference between array_slice and array_splice in PHP?
easy
A. array_slice returns a new array without changing the original, while array_splice modifies the original array.
B. array_slice modifies the original array, while array_splice returns a new array without changes.
C. Both functions modify the original array but in different ways.
D. Both functions return a new array and do not modify the original array.

Solution

  1. Step 1: Understand array_slice behavior

    array_slice extracts a portion of the array and returns it as a new array without changing the original array.
  2. Step 2: Understand array_splice behavior

    array_splice removes or replaces elements in the original array and returns the removed elements, thus modifying the original array.
  3. Final Answer:

    array_slice returns a new array without changing the original, while array_splice modifies the original array. -> Option A
  4. Quick Check:

    Slice copies, splice changes original [OK]
Hint: Slice copies array parts, splice changes the original array [OK]
Common Mistakes:
  • Thinking both functions modify the original array
  • Confusing which function returns a new array
  • Assuming splice returns the modified array instead of removed elements
2. Which of the following is the correct syntax to remove 2 elements starting from index 1 using array_splice on array $arr?
easy
A. array_splice($arr, 2, 1);
B. array_splice($arr, 1);
C. array_splice($arr, 1, 2);
D. array_splice($arr, 0, 2);

Solution

  1. Step 1: Identify parameters of array_splice

    The function signature is array_splice(array &$array, int $offset, int $length = ?). To remove 2 elements starting at index 1, offset = 1 and length = 2.
  2. Step 2: Match parameters to options

    array_splice($arr, 1, 2); uses array_splice($arr, 1, 2); which correctly removes 2 elements starting from index 1.
  3. Final Answer:

    array_splice($arr, 1, 2); -> Option C
  4. Quick Check:

    Offset 1, length 2 removes correct elements [OK]
Hint: array_splice($arr, start, length) removes elements from start [OK]
Common Mistakes:
  • Swapping offset and length values
  • Omitting length parameter and removing all after offset
  • Using wrong index for offset
3. What will be the output of the following PHP code?
$arr = [10, 20, 30, 40, 50];
$result = array_slice($arr, 2, 2);
print_r($result);
medium
A. [30, 40]
B. [20, 30]
C. [40, 50]
D. [10, 20]

Solution

  1. Step 1: Understand array_slice parameters

    It extracts elements starting at index 2, taking 2 elements from $arr.
  2. Step 2: Extract elements from the array

    Index 2 is 30, next element is 40, so the slice is [30, 40].
  3. Final Answer:

    [30, 40] -> Option A
  4. Quick Check:

    Slice from index 2, length 2 = [30, 40] [OK]
Hint: array_slice returns a new array from offset with given length [OK]
Common Mistakes:
  • Confusing index start point
  • Assuming original array is changed
  • Mixing up array_slice with array_splice
4. Consider this code snippet:
$arr = [1, 2, 3, 4, 5];
array_splice($arr, 2, 2, [8, 9]);
print_r($arr);

What is the output?
medium
A. [1, 2, 3, 4, 8, 9, 5]
B. [1, 2, 3, 4, 5]
C. [8, 9, 5]
D. [1, 2, 8, 9, 5]

Solution

  1. Step 1: Understand array_splice with replacement

    The call removes 2 elements starting at index 2 (elements 3 and 4) and inserts [8, 9] at that position.
  2. Step 2: Modify the original array accordingly

    Original array: [1, 2, 3, 4, 5]
    Remove elements at index 2 and 3: [3, 4]
    Insert [8, 9] at index 2
    Resulting array: [1, 2, 8, 9, 5]
  3. Final Answer:

    [1, 2, 8, 9, 5] -> Option D
  4. Quick Check:

    Splice replaces elements with new ones [OK]
Hint: Splice can replace elements by passing replacement array [OK]
Common Mistakes:
  • Expecting original array unchanged
  • Forgetting splice removes elements before inserting
  • Confusing splice insertion with append
5. You have an array $data = [5, 10, 15, 20, 25, 30]. You want to remove the middle two elements and insert 100 and 200 in their place, then get a new array with the last three elements without changing the original array. Which code snippet correctly does this?
hard
A.
array_splice($data, 2, 2, [100, 200]);
$copy = array_slice($data, -3);
print_r($copy);
B.
$copy = array_slice($data, -3);
array_splice($data, 2, 2, [100, 200]);
print_r($copy);
C.
$copy = array_splice($data, -3);
array_splice($data, 2, 2, [100, 200]);
print_r($copy);
D.
$copy = array_slice($data, 2, 3);
array_splice($data, 2, 2, [100, 200]);
print_r($copy);

Solution

  1. Step 1: Extract last three elements without changing original

    Using array_slice($data, -3) copies last three elements [20, 25, 30] into $copy without modifying $data.
  2. Step 2: Modify original array by replacing middle two elements

    array_splice($data, 2, 2, [100, 200]) removes elements at index 2 and 3 (15, 20) and inserts 100 and 200.
  3. Step 3: Print the copied array

    Printing $copy shows the last three elements of the original array before modification.
  4. Final Answer:

    $copy = array_slice($data, -3); array_splice($data, 2, 2, [100, 200]); print_r($copy); -> Option B
  5. Quick Check:

    Slice first to copy, then splice to modify [OK]
Hint: Slice first to copy, then splice to modify original [OK]
Common Mistakes:
  • Using splice to copy array (modifies original)
  • Modifying array before slicing (copy changes)
  • Wrong slice parameters for last elements
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