What are the best practices for persisting complex data structures, like those used in a product configurator, in PHP sessions?

When persisting complex data structures, like those used in a product configurator, in PHP sessions, it's important to serialize the data before storing it and unserialize it when retrieving it. This ensures that the data structure is maintained properly across session requests. Additionally, using session variables with descriptive keys can help keep track of different parts of the data structure.

// Serialize and store complex data structure in session
$complexData = ['product' => 'example', 'options' => ['color' => 'red', 'size' => 'medium']];
$_SESSION['config_data'] = serialize($complexData);

// Retrieve and unserialize complex data structure from session
$complexData = unserialize($_SESSION['config_data']);