What is the purpose of using array_slice in PHP and how can it be utilized effectively?
The purpose of using array_slice in PHP is to extract a portion of an array, starting from a specified index and with a specified length. This function can be utilized effectively when you need to work with only a subset of elements from a larger array without modifying the original array.
// Sample array
$originalArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Extracting a portion of the array starting from index 2 with a length of 3
$subsetArray = array_slice($originalArray, 2, 3);
// Output the subset array
print_r($subsetArray);