reading CSV file with PHP


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.