It returns a portion of an array without modifying the original array. You specify the start position and length of the slice.
Click to reveal answer
beginner
How does array_splice() differ from array_slice()?
array_splice() removes a portion of an array and can replace it with new elements. It modifies the original array.
Click to reveal answer
intermediate
What happens if you use a negative start index in array_slice()?
The slice starts that many elements from the end of the array. For example, -2 means start from the second last element.
Click to reveal answer
intermediate
What does array_splice($array, 2, 3, ['a', 'b']) do?
It removes 3 elements from $array starting at index 2, then inserts 'a' and 'b' at that position, modifying $array.
Click to reveal answer
intermediate
Does array_slice() preserve keys by default?
No, by default it reindexes the returned array starting from 0. You can preserve keys by passing true as the fourth argument.
Click to reveal answer
Which function modifies the original array in PHP?
Aarray_map()
Barray_slice()
Carray_merge()
Darray_splice()
✗ Incorrect
array_splice() changes the original array by removing and optionally replacing elements.
What does array_slice($arr, 1, 2) return?
ATwo elements starting from index 1
BElements from index 1 to 2 inclusive
CAll elements except the first
DThe entire array
✗ Incorrect
It returns 2 elements starting at index 1, without modifying the original array.
How do you preserve keys when using array_slice()?
APass true as the fourth argument
BPass false as the third argument
CUse array_splice() instead
DKeys are always preserved
✗ Incorrect
Passing true as the fourth argument preserves the original keys in the returned slice.
What does array_splice($arr, 0, 0, ['x']) do?
ARemoves the first element
BInserts 'x' at the start without removing elements
CReplaces the first element with 'x'
DReturns an empty array
✗ Incorrect
It inserts 'x' at the start without removing any elements because length is 0.
If you want to get the last 3 elements of an array without changing it, which is best?
Aarray_splice($arr, -3)
Barray_shift() three times
Carray_slice($arr, -3)
Darray_pop() three times
✗ Incorrect
array_slice($arr, -3) returns the last 3 elements without modifying the original array.
Explain the difference between array_slice() and array_splice() in PHP.
Think about whether the original array changes or not.
You got /4 concepts.
Describe how to use array_splice() to remove elements and insert new ones.
Remember it works like cutting out and pasting in new pieces.
You got /4 concepts.
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
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.
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.
Final Answer:
array_slice returns a new array without changing the original, while array_splice modifies the original array. -> Option A
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
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.
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.
Final Answer:
array_splice($arr, 1, 2); -> Option C
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?
The call removes 2 elements starting at index 2 (elements 3 and 4) and inserts [8, 9] at that position.
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]
Final Answer:
[1, 2, 8, 9, 5] -> Option D
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?