How can sessions be used to handle arrays in PHP?

To handle arrays in PHP sessions, you can serialize the array before storing it in the session and unserialize it when retrieving it. This allows you to store complex data structures like arrays in session variables and maintain their structure when accessing them later.

// Start the session
session_start();

// Define an array
$array = ['apple', 'banana', 'cherry'];

// Serialize the array and store it in the session
$_SESSION['myArray'] = serialize($array);

// Retrieve the array from the session and unserialize it
$retrievedArray = unserialize($_SESSION['myArray']);

// Output the array
print_r($retrievedArray);