Complete the code to get a slice of the array from index 1 to 3.
<?php $fruits = ['apple', 'banana', 'cherry', 'date', 'fig']; $slice = array_slice($fruits, [1], 3); print_r($slice); ?>
The array_slice function extracts a portion of the array starting at the given index. To start from the second element (index 1), use 1.
Complete the code to remove 2 elements from the array starting at index 2 using array_splice.
<?php $colors = ['red', 'green', 'blue', 'yellow', 'purple']; array_splice($colors, [1], 2); print_r($colors); ?>
The array_splice function removes elements starting at the given index. To remove from the third element, use index 2.
Fix the error in the code to correctly insert 'orange' at index 1 without removing elements.
<?php $fruits = ['apple', 'banana', 'cherry']; array_splice($fruits, [1], 0, 'orange'); print_r($fruits); ?>
To insert without removing, set the length parameter to 0 and specify the index where you want to insert. Index 1 inserts before 'banana'.
Fill both blanks to create a slice of the array from index 2 to the end.
<?php $numbers = [10, 20, 30, 40, 50]; $slice = array_slice($numbers, [1], [2]); print_r($slice); ?>
Starting at index 2 with length 3 extracts [30, 40, 50] from the third item to the end.
Fill all three blanks to remove 1 element at index 1 and insert 'kiwi' and 'mango' instead.
<?php $fruits = ['apple', 'banana', 'cherry', 'date']; array_splice($fruits, [1], [2], [[3]]); print_r($fruits); ?>
Start at index 1, remove 1 element ('banana'), and insert 'kiwi' and 'mango' as an array.