Jump to content

[SOLVED] Read CSV File


timmah1

Recommended Posts

<?php
$file = "contacts.csv";
$contents = file_get_contents($file);
$rows = explode('\n', $contents);
foreach ($rows as $k=>$v) {
       if ($k == 0) { continue; } //header row, don't need it.
       $data = explode(',', $v);
       print_r($data);
}
?>

 

Just an example to get you started.

Link to comment
https://forums.phpfreaks.com/topic/118495-solved-read-csv-file/#findComment-610032
Share on other sites

ok, that fixed the problem.

 

Now, how do I get it to not show as arrary's?

 

Just 3 headings, First Name, Last Name, E-mail Address

 

So that it would like this:

test test [email protected]

 

right now, it look like this

Array ( [0] => test [1] => test [2] => [email protected] )

Link to comment
https://forums.phpfreaks.com/topic/118495-solved-read-csv-file/#findComment-610060
Share on other sites

Simple. Instead of print_r()'ing it, echo the implode()'d array:

 

<?php
$file = "contacts.csv";
$contents = file_get_contents($file);
$rows = explode("\n", $contents);
foreach ($rows as $k=>$v) {
       if ($k == 0) { continue; } //header row, don't need it.
       $data = explode(',', $v);
       echo implode(' ', $data), '<br />';
}
?>

 

Or you could simply replace the commas with spaces, inside the foreach loop.

Link to comment
https://forums.phpfreaks.com/topic/118495-solved-read-csv-file/#findComment-610083
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.