How can PHP serialize and unserialize functions be used to handle complex data structures like arrays?

PHP serialize and unserialize functions can be used to handle complex data structures like arrays by converting the array into a string representation that can be stored or transmitted easily. To serialize an array, you can use the serialize function, which will convert the array into a string. To unserialize the serialized string back into an array, you can use the unserialize function. This allows you to store complex data structures like arrays in databases or pass them between different parts of your code.

// Serialize an array
$data = ['name' => 'John', 'age' => 30, 'city' => 'New York'];
$serializedData = serialize($data);

// Store or transmit the serialized data

// Unserialize the data back into an array
$unserializedData = unserialize($serializedData);

// Access the array elements
echo $unserializedData['name']; // Output: John
echo $unserializedData['age']; // Output: 30
echo $unserializedData['city']; // Output: New York