When storing query results in an array in PHP, what considerations should be taken into account to ensure efficient memory usage?

When storing query results in an array in PHP, it's important to consider the amount of data being fetched and stored in memory. To ensure efficient memory usage, you can limit the number of rows fetched from the database at a time and process them in chunks. This approach helps prevent memory exhaustion when dealing with large datasets.

// Example of storing query results in an array with memory-efficient chunk processing

// Define chunk size
$chunkSize = 1000;

// Query to fetch data
$query = "SELECT * FROM table_name";
$result = mysqli_query($connection, $query);

// Process data in chunks
while ($rows = mysqli_fetch_all($result, MYSQLI_ASSOC, $chunkSize)) {
    // Process each chunk of data
    foreach ($rows as $row) {
        // Do something with the data
    }
}