Recall & Review
beginner
What does the
array_slice() function do in PHP?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?
✗ Incorrect
array_splice() changes the original array by removing and optionally replacing elements.What does
array_slice($arr, 1, 2) return?✗ Incorrect
It returns 2 elements starting at index 1, without modifying the original array.
How do you preserve keys when using
array_slice()?✗ Incorrect
Passing true as the fourth argument preserves the original keys in the returned slice.
What does
array_splice($arr, 0, 0, ['x']) do?✗ 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?
✗ 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.