What are alternative methods, such as array_slice, that can be used to achieve the desired outcome in PHP programming?

When working with arrays in PHP, sometimes we need to extract a portion of an array based on a specific range of keys. One way to achieve this is by using the array_slice function. This function allows us to extract a slice of an array based on the starting index and length provided.

// Sample array
$array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Extract a slice of the array starting from index 2 and length 4
$slice = array_slice($array, 2, 4);

print_r($slice);