Posted By

antpaw on 06/22/09


Tagged

php csv


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

vali29
irishsk
nublaii
BrianCoyDesign


reading CSV file with PHP


 / Published in: PHP
 

this script splits the csv file two times (1. on "new line", 2. on ";") and returns a two dem array in the last for loop.

  1. <?php
  2. /*
  3. your CSV should look like this:
  4. data1;data2;data3;
  5. data1;data2124;data3;
  6. data1;data2564;data3;
  7. data1;data2321;data3;
  8. */
  9.  
  10. $fileCont = join('', file('myCSV.csv'));
  11. $fileArrN = explode("\n", $fileCont);
  12.  
  13. $a = 0;
  14. foreach ($fileArrN as $value){
  15. $fileArrSemi[$a] = explode(";", $value);
  16. $a++;
  17. }
  18.  
  19. $output = null;
  20. for($i = 0; $i < sizeof($fileArrSemi); $i++){
  21. $output .= $fileArrSemi[$i][1]."<br />\n";
  22. }
  23.  
  24. echo $output;
  25. ?>

Report this snippet  

You need to login to post a comment.