PHP Version
4+
// Original array
$array = ['a', 'b', 'c', 'd', 'e'];

/*
    Use the array_slice() function to extract
    three elements starting from index 1
    from the original array and return the result
    as a new array.
*/
$sliced_array = array_slice($array, 1, 3);

print_r($sliced_array);
/*
Output:
    Array
    (
        [0] => b
        [1] => c
        [2] => d
    )
*/
array_slice(
    array $array,
    int $offset,
    ?int $length = null,
    bool $preserve_keys = false
): array

/* array_slice(
    the original array,
    the index at which to start extracting elements[,
    the number of elements to extract[,
    a boolean value indicating whether to preserve the keys]]
   );
*/
print_r(array_slice($indexed_arr, 0));      // Extract all elements
print_r(array_slice($indexed_arr, 2));      // From index 2 ('C') to the end
print_r(array_slice($indexed_arr, 2, 1));   // One element starting from index 2
print_r(array_slice($indexed_arr, -2, 1));  // One element starting from the second-to-last ('D')
print_r(array_slice($indexed_arr, 7));      // Out of range returns an empty array: Array ( )

// Comparison of key preservation
print_r(array_slice($indexed_arr, 2, 2));        // Keys reindexed: [0] => C, [1] => D
print_r(array_slice($indexed_arr, 2, 2, true));  // Keys preserved: [2] => C, [3] => D
Output
$associative_arr = [
    'a' => 'red',
    'b' => 'green',
    'c' => 'blue',
    'd' => 'olive',
    'e' => 'brown'
];

print_r(array_slice($associative_arr, 0)); // Array ( [a] => red [b] => green [c] => blue [d] => olive [e] => brown )
print_r(array_slice($associative_arr, 2)); // Array ( [c] => blue [d] => olive [e] => brown )
print_r(array_slice($associative_arr, 2, 1)); // Array ( [c] => blue )
print_r(array_slice($associative_arr, -2, 1)); // Array ( [d] => olive )
print_r(array_slice($associative_arr, 7)); // Array ( )

// Difference in key preservation based on the third parameter
// In associative arrays, keys are always preserved regardless of this parameter.
print_r(array_slice($associative_arr, 2, 2)); // Array ( [c] => blue [d] => olive )
print_r(array_slice($associative_arr, 2, 2, true)); // Array ( [c] => blue [d] => olive )
// Log data array
$logData = [
    '2022-01-01' => 'Log entry 1',
    '2022-01-02' => 'Log entry 2',
    /* ... */
];

// Extract log data for the most recent 5 days
$recentLogs = array_slice($logData, 0, 5);

// Output the result
print_r($recentLogs);
// Product list array
$products = [
    101 => 'Laptop',      // Index 0
    102 => 'Smartphone',  // Index 1 (starting point!)
    103 => 'Headphones',  // Index 2
    104 => 'Tablet',      // Index 3
    105 => 'Camera',      // Index 4
    106 => 'Smartwatch',  // Index 5
];

// Extract 3 elements starting from the second element (index 1)
// Set $preserve_keys to true to preserve the original keys (102, 103, 104).
$selectedProducts = array_slice($products, 1, 3, true);

// Output the result
print_r($selectedProducts);

/*
Output:
Array
(
    [102] => Smartphone
    [103] => Headphones
    [104] => Tablet
)
*/
// Recent activity list array
$recentActivity = [
    'Commented on a post',
    'Liked a photo',
    'Posted a status',
    'Followed a user',
    'Shared a link',
];

// Extract the most recent 3 activities
$selectedActivity = array_slice($recentActivity, -3, null, true);

// Output the result
print_r($selectedActivity);