Site Tools


Hotfix release available: 2025-05-14b "Librarian". upgrade now! [56.2] (what's this?)
Hotfix release available: 2025-05-14a "Librarian". upgrade now! [56.1] (what's this?)
New release available: 2025-05-14 "Librarian". upgrade now! [56] (what's this?)
prog:php:samples:csv_parse

One liner to get a CSV file into 2-dimentional array NB! Only works if “,” is the separator!!! NB! Does not work if CSV cells have line breaks in them!!!

$csv = array_map('str_getcsv', file('data.csv'));

Work with each line in a CSV file:

$handle = fopen("data_sample_5_sec.csv", "r");
 
$is_first_row_header = true;
 
for ($i = 0; $row = fgetcsv($handle, null, ";"); ++$i) {
    if ($is_first_row_header AND $i == 0) {
        $headers = $row;
        continue;
    }
 
    // Do something with $row array
    $row[''];
}
fclose($handle);
prog/php/samples/csv_parse.txt · Last modified: 2023/11/10 16:01 by rene