How can the unset function be used in PHP to remove a specific element from an array stored in a cookie?

To remove a specific element from an array stored in a cookie in PHP, you can use the unset function to unset the element by its key. This will effectively remove the element from the array stored in the cookie. You need to retrieve the array from the cookie, unset the specific element, and then update the cookie with the modified array.

// Retrieve the array stored in the cookie
$cookieData = $_COOKIE['cookie_name'];

// Convert the cookie data to an array
$cookieArray = unserialize($cookieData);

// Unset the specific element from the array
unset($cookieArray['key_to_remove']);

// Update the cookie with the modified array
setcookie('cookie_name', serialize($cookieArray), time() + 3600, '/');