How can the PHP code be optimized to efficiently handle the deletion of items from the shopping cart without affecting the rest of the array?

When deleting items from a shopping cart in PHP, it is important to efficiently handle the removal of the item without affecting the rest of the array. One way to optimize this process is by using the unset() function to remove the specific item from the array without reindexing the remaining elements.

// Assume $cart is the array representing the shopping cart
$itemToRemove = 'item_id_to_remove';

if (($key = array_search($itemToRemove, $cart)) !== false) {
    unset($cart[$key]);
}