What is the correct way to delete a value from an array in PHP?

When deleting a value from an array in PHP, you can use the unset() function to remove a specific element by its key. This function will completely remove the element from the array, shifting the keys of subsequent elements to fill the gap left by the removed element.

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

unset($array[$key_to_delete]);

print_r($array);