Show csv file in array

I have. CSV file. Some fields are empty, and some fields have commas or periods.

Now, how could I display it in this array?

Array ( [0] => Name1, name1@yahoo.com ,13,56475,,190000,,,remit,5-Mar-15,26250 [1] => Name2, name2@yahoo.com ,11,11250,,22500,,,the bank,30-Apr-15,6250 [2] => Name3, name3@yahoo.com ,15,,,1500,receipt,,the market,7-Mar-15,1750 [3] =>Name4, name4@yahoo.com ,21,25500,,46750,receipt,,bank ,7-Mar-15,21350 ..... ) 

At the moment, I can only display it like this:

 Array ( [0] => Name1 [1] => name1@yahoo.com [2] => 13 [3] => 56475 [4] => [5] => 19000 [6] => [7] => [8] => remit [9] => 5-Mar-15 [10] => 26250 ) Array ( [0] => Name2 [1] => name2@yahoo.com [2] => 11 [3] => 11250 [4] => [5] => 22500 [6] => [7] => [8] => the bank [9] => 30-Apr-15 [10] => 6250 ) .... 

And I use this code:

 $file = fopen("pay.csv","r"); while(! feof($file)) { echo '<pre>',print_r(fgetcsv($file)),'</pre>'; } fclose($file); 

I prefer to use my own csv functions.

+5
source share
4 answers

If your expected output is above, you can simply use the file() method to parse the file in the array.

 $lines = file('file.csv'); foreach ($lines as $line_num => $line) { echo $line . '<br />'; } 
+2
source

fgetcsv () may solve your problem.

The fgetcsv () function parses a line from an open file by checking the CSV fields.

  • The fgetcsv () function stops returning on a new line, on a specified length, or in EOF, whichever comes first.

  • This function returns the CSV fields in the array with success, or FALSE on failure and EOF.

Syntax

fgetcsv (file, length, separator, enclosure)

  • file . Specifies the file to check.
  • length . Indicates the maximum string length. Must be longer than the longest line (in characters) in the CSV file. Omitting this (or setting it to 0) the line length is not limited, which is slightly slower.

Example:

 <?php $file = fopen("contacts.csv","r"); print_r(fgetcsv($file)); fclose($file); ?> 

CSV file:

 Kai Jim, Refsnes, Stavanger, Norway Hege, Refsnes, Stavanger, Norway 

Code output above:

 Array ( [0] => Kai Jim [1] => Refsnes [2] => Stavanger [3] => Norway ) 

Visit http://php.net/manual/en/function.fgetcsv.php for more information.

+1
source

Following this example: http://php.net/manual/en/function.fgetcsv.php#example-2613

 if (($handle = fopen("test.csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $filteredData = array_filter($data, function($item) { return '' !=== $item; }); echo implode(', ', $filteredData), PHP_EOL; } fclose($handle); } 

But instead of printing implode(', ', $filteredData) , delete array_filter() and print implode('</span><span class="comma">', $data) with the following CSS:

 .comma { background-color: red; padding: 2px 5px; } .comma:empty { display: none; } .comma + .comma { margin-left: 20px; } 

New PHP code:

 if (($handle = fopen("test.csv", "r")) !== FALSE) { echo '<div class="csv">'; while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { echo '<div><span class="comma">'; echo implode('</span>, <span class="comma">', $data), PHP_EOL; echo '</span></div>'; } fclose($handle); echo '</div>'; } 
+1
source

CSV files are read by separating each value with a comma. if you stand, contains the coma itself, then it is considered as a plural value example if you have the value B, C and the value A if you write css as B, C, A, then it considers itself as BC and A to avoid such a problem. for example, quotation marks or double quotation marks "B, C", "A"

0
source

All Articles