Skip to content
PHP Logo

How can the array_chunk function in PHP be used to organize and manipulate data from CSV files, and what are some common use cases for this approach in data processing tasks?

To organize and manipulate data from CSV files using the array_chunk function in PHP, you can read the CSV file into an array, then use array_chunk to split the data into smaller chunks for processing. This can be useful for tasks such as batch processing, data analysis, or generating reports from large CSV files.

php
// Read CSV file into an array
$csvData = array_map('str_getcsv', file('data.csv'));

// Split data into chunks of 100 rows
$chunks = array_chunk($csvData, 100);

// Process each chunk
foreach ($chunks as $chunk) {
    // Perform data manipulation or analysis on each chunk
}