How can the array_splice function be used effectively to delete elements from an array in PHP?

To delete elements from an array in PHP, you can use the array_splice function. This function allows you to remove a portion of an array and replace it with something else if needed. To delete elements, you can specify the start index and the number of elements to remove.

$array = [1, 2, 3, 4, 5];
$startIndex = 2;
$numberOfElements = 2;

array_splice($array, $startIndex, $numberOfElements);

print_r($array); // Output: [1, 2, 5]