I have a csv file which looks like this
$lines[0] = "text, with commas", "another text", 123, "text",5;
$lines[1] = "some without commas", "another text", 123, "text";
$lines[2] = "some text with commas or no",, 123, "text";
And I would like to have a table:
$t[0] = array("text, with commas", "another text", "123", "text","5");
$t[1] = array("some without commas", "another text", "123", "text");
$t[2] = array("some text, with comma,s or no", NULL , "123", "text");
If I use split($lines[0],",")
I'll get "text" ,"with commas" ...
Is there any elegant way to do it?
You could use something like https://github.com/htmlburger/carbon-csv that allows column mapping:
The result of the below code would be something like:
Another library that does the same thing(and much more) is http://csv.thephpleague.com/9.0/reader/
Suppose you have a create a function for same things, Then it should look like
You can use
fgetcsv
to parse a CSV file without having to worry about parsing it yourself.Example from PHP Manual:
When you want to keep the index (first line) for multidimensional result array, you can use:
In addition to Matt's suggestion, you can also use
SplFileObject
to read in the file:source: http://de.php.net/manual/en/splfileobject.setcsvcontrol.php
you can read the data using the following function.
http://www.pearlbells.co.uk/how-to-sort-a1a2-z9z10aa1aa2-az9az10-using-php/