In PHP, how can you ensure that an array remains sorted after removing an element?
When removing an element from an array in PHP, the remaining elements may not be in sorted order. To ensure that the array remains sorted after removing an element, you can re-sort the array using functions like `sort()` or `asort()` after the removal operation. This will rearrange the elements in the array in a sorted order.
// Original array
$array = [4, 2, 7, 1, 5];
// Remove an element
$elementToRemove = 7;
$key = array_search($elementToRemove, $array);
if($key !== false){
unset($array[$key]);
}
// Re-sort the array
sort($array);
// Output the sorted array
print_r($array);