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

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
prog:php:samples:csv_parse [2022/03/15 13:10] reneprog:php:samples:csv_parse [2023/11/10 16:01] (current) rene
Line 1: Line 1:
-One liner to get a CSV file into 2-dimentional array:+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!!!
 <code php> <code php>
 $csv = array_map('str_getcsv', file('data.csv')); $csv = array_map('str_getcsv', file('data.csv'));
Line 11: Line 13:
 $is_first_row_header = true; $is_first_row_header = true;
  
-for ($i = 0; $row = fgetcsv($handle ); ++$i) {+for ($i = 0; $row = fgetcsv($handle, null, ";"); ++$i) {
     if ($is_first_row_header AND $i == 0) {     if ($is_first_row_header AND $i == 0) {
         $headers = $row;         $headers = $row;
 +        continue;
     }     }
  
-    // Do something will $row array+    // Do something with $row array
     $row[''];     $row[''];
 } }
 fclose($handle); fclose($handle);
 </code> </code>
prog/php/samples/csv_parse.1647342608.txt.gz · Last modified: 2022/03/15 13:10 by rene