What potential pitfalls should be avoided when trying to delete arrays within arrays in PHP sessions?

When trying to delete arrays within arrays in PHP sessions, it's important to avoid directly modifying the session data as it can lead to unexpected behavior or data corruption. Instead, it's recommended to retrieve the session data, make the necessary modifications, and then update the session data with the new values.

// Retrieve session data
session_start();
$data = $_SESSION['data'];

// Delete arrays within arrays
unset($data['array1']['array2']);

// Update session data with modified values
$_SESSION['data'] = $data;