What are the potential pitfalls of using arrays in PHP, especially in relation to memory limits?

Using large arrays in PHP can quickly consume memory, especially when dealing with large datasets. This can lead to memory limit errors and performance issues. To mitigate this, consider using generators instead of arrays for processing large datasets. Generators allow you to iterate over a large dataset without storing it entirely in memory, thus reducing memory usage.

// Example of using a generator instead of an array to process a large dataset
function largeDataSetGenerator() {
    // Generate data or fetch from a database
    for ($i = 0; $i < 1000000; $i++) {
        yield $i;
    }
}

// Iterate over the generator
foreach (largeDataSetGenerator() as $data) {
    // Process data without storing the entire dataset in memory
    echo $data . PHP_EOL;
}