How To Read A CSV With PHP

Posted on Monday, December 21, 2020 @ 11:21:24 UTC in PHP
by neralex

southern writes:  

Looking how to read a CSV using PHP? What to import a CSV into our database? The code snippet below will help you do just that.

PHP.net has the manual for the fgetcsv which is used in this example.

Example

$row = 1; if (($handle = fopen("test.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "

$num fields in line $row:

\n"; $row++; for ($c=0; $c \n"; } } fclose($handle); }

Replace test.csv with your own file.

This snippet will parse CSV into a multidimensional array

*All code snippets are open source, everyone is free to use.

PhP Tricks

 
 
click Related        click Share
 
News ©

Site Info

Last SeenLast Seen
  • hicuxunicorni...
  • dagendy
Server TrafficServer Traffic
  • Total: 491,090,816
  • Today: 11,251
Server InfoServer Info
  • Oct 04, 2024
  • 02:03 pm UTC