How can array_slice() be effectively used to extract multiple entries before and after a specific value in a PHP array?
When you need to extract multiple entries before and after a specific value in a PHP array, you can use array_slice() to achieve this. You can find the index of the specific value using array_search(), and then use array_slice() to extract the desired number of elements before and after that index.
// Sample array
$array = [10, 20, 30, 40, 50, 60, 70, 80, 90];
// Find the index of the specific value
$index = array_search(50, $array);
// Extract 2 elements before and after the specific value
$before = array_slice($array, max(0, $index - 2), 2);
$after = array_slice($array, $index + 1, 2);
// Output the extracted elements
print_r($before);
print_r($after);