What is the best way to store and retrieve a multidimensional array in a text file in PHP?
Storing and retrieving a multidimensional array in a text file in PHP can be achieved by serializing the array before writing it to the file and then unserializing it when reading it back. This allows us to maintain the structure and data of the array in a text format that can be easily stored and retrieved.
// Sample multidimensional array
$array = array(
array('John', 'Doe'),
array('Jane', 'Smith')
);
// Serialize the array and write it to a text file
$serializedArray = serialize($array);
file_put_contents('data.txt', $serializedArray);
// Read the serialized array from the text file and unserialize it
$serializedArray = file_get_contents('data.txt');
$unserializedArray = unserialize($serializedArray);
// Output the unserialized array
print_r($unserializedArray);