What are the best practices for managing memory usage when working with large arrays in PHP functions?

When working with large arrays in PHP functions, it's important to manage memory usage efficiently to avoid performance issues. One way to do this is by using generators instead of arrays to process data in chunks, reducing memory consumption. Additionally, unset() can be used to free up memory after processing large arrays.

// Example of using generators to process large arrays in chunks
function processLargeArray($largeArray) {
    foreach (array_chunk($largeArray, 1000) as $chunk) {
        foreach ($chunk as $item) {
            // Process each item
        }
        unset($chunk); // Free up memory after processing each chunk
    }
}

// Usage
$largeArray = range(1, 1000000); // Example large array
processLargeArray($largeArray);