What are the potential pitfalls of using arrays to store large amounts of data in PHP?

One potential pitfall of using arrays to store large amounts of data in PHP is that it can consume a lot of memory, especially if the array is multidimensional or contains a large number of elements. This can lead to performance issues and potentially cause the script to run out of memory. To solve this issue, consider using alternative data structures such as databases or streams to store and access large amounts of data more efficiently.

// Example of using streams to store and access large amounts of data
$stream = fopen('data.txt', 'w+');

$data = ['apple', 'banana', 'cherry', 'date', 'elderberry'];

foreach ($data as $item) {
    fwrite($stream, $item . PHP_EOL);
}

rewind($stream);

while (($line = fgets($stream)) !== false) {
    echo $line;
}

fclose($stream);