Instantly share code, notes, and snippets.
-
Star
(262)
You must be signed in to star a gist -
Fork
(79)
You must be signed in to fork a gist
-
-
Save jaywilliams/385876 to your computer and use it in GitHub Desktop.
| name | number | |
|---|---|---|
| Lorem | 11 | |
| ipsum | 22 |
iqbalatblackid
commented
Feb 24, 2015
Perfect ......
sommarnatt
commented
Apr 2, 2015
Thanks @rboatright
mokanfar
commented
May 21, 2015
<3
Namsep
commented
Sep 16, 2015
All my columns are double quotes ( "name something") and are nicely mapped. Except for the first one, which is correct in the CSV. Anyone got a clue?
[0] => "Contract Number"
[1] => Product Type
[2] => Internal Title
KarelWintersky
commented
Oct 11, 2015
Excellent. Thanx!
irfanwv
commented
Apr 26, 2016
What if in the Name: Lorem, Ipsum
and Id: 12
janepatel
commented
Jun 6, 2016
Hi
If you want to convert text file into array please follow below link, on this link you can find complete code with live demo.
You can try online with link link.
rap2hpoutre
commented
Jun 9, 2016
I just made an ugly tiny version for composer / packagist (remember left-pad for JS, yolo)
composer require rap2hpoutre/csv-to-associative-array
botaylortravismathew
commented
Mar 27, 2017
This is awesome! I used it for importing large amount of data into a Wordpress ACF Repeater field. Worked great thank you very much for this.
@rboatright
I don't know why I didn't notice this before, but you aren't using the $delimiter passed as a parameter for the header columns. Wouldn't they usually be the same delimiter as the rest of the file?
Edit: Doh! I remember having thought about this last time. I see what it's doing now. I'm passing in the "headers" via string which is comma delimited. Thanks!
arodasmedia
commented
Jun 23, 2017
Hi guys,
I have this result:
Array ( [0] => Array ( [cod_piesa] => gl [ lungime] => 5000 [ latime] => 3000 [ inaltime] => 150 ) [1] => Array ( [cod_piesa] => fd [ lungime] => 3000 [ latime] => 2000 [ inaltime] => 100 ) )
What I would like to do is to insert the above results in a table like this:
$line = 0
$part = GL
$cota = lungime
$value = 5000
then to insert another row in mysql
$line = 0
$part = GL
$cota = latime
$value = 3000
and so on with each line basically I have a component code with different dimensions, instead of inserting in a huge table in mysql I want to add it as a list with while or something.
Thank you very much!
arodasmedia
commented
Jun 23, 2017
This is what I have in the csv
cod_piesa, lungime, latime, inaltime
gl, 5000, 3000, 150
fd, 3000, 2000, 100
jeffersongouveia
commented
Aug 4, 2017
Thank you very much, saved me a lot of time and probably got better than what I would do.
KarelWintersky
commented
Sep 20, 2017
A little trick:
$formatter = function($value, $delim = ',', $enclosure = '"', $escape = '\\') use (&$header) {
if (!$header) {
$header = array_map('trim', str_getcsv($value, $delim, $enclosure, $escape));
return $header;
} else {
return array_combine($header, array_map('trim', str_getcsv($value, $delim, $enclosure, $escape)));
}
};
$csv = array_map($formatter, file('data.csv'), array() );
than, first element of array (index 0) will be like this:
[0]=>
array(2) {
[0]=>
string(4) "name"
[1]=>
string(6) "number"
}
unset($csv[0]); and you will recieve useful two-dimensional array
shyamgarud
commented
Oct 24, 2017
Thank you vary much!
OliverLeitner
commented
Jan 23, 2018
thanks alot, this is exaxtly the function i was looking for.
debianx
commented
Mar 18, 2018
Perfect, Thank.
zhanls
commented
Jul 1, 2018
Thanks for the function, it works like a charm.
virenbpanchal
commented
Nov 13, 2018
Thanks you very much. I was looking for a solution about to insert CSV file data which contains comma(,) in most of the columns values. I used Csvreader library but by using that I got data by exploding from comma(,) in column value. Now I got your function csv_to_array() and it solved my problem.
hAbd0u
commented
Jul 16, 2019
All my columns are double quotes ( "name something") and are nicely mapped. Except for the first one, which is correct in the CSV. Anyone got a clue?
[0] => "Contract Number" [1] => Product Type [2] => Internal Title
This happen because your csv file has special encoding, first you need to know what is it, than remove it. In my case was UTF-BOM, so the final version would be like this:
function csv_to_array($filename='', $delimiter=',', $encoding = NULL)
{
if(!file_exists($filename) || !is_readable($filename))
return FALSE;
$header = NULL;
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
if($encoding) {
$header = preg_replace('/' . $encoding . '/', '', $row);
}
else
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
return $data;
}
Example:
print_r(csv_to_array('example.csv',,'[\x80-\xFF]')); // This will remove UTF-8 encoding
print_r(csv_to_array('example.csv', ',','[\x80-\xFF]'));
despe4er
commented
Jan 10, 2020
Thanks!
mlpassos
commented
Feb 18, 2020
I have tried something like 3 different libraries for codeigniter with no success. Yours is the first one that works perfect without a touch.
THANK YOU!
Hi Thanks for the script :)
I will like to know how to make a foreach loop through the array? because I will like to echo the result out in a table
Sure, here's a simple, primitive example (code untested):
<?php $data = csv_to_array('example.csv'); echo "<table>"; foreach ($data as $row) { echo "<tr>"; foreach ($row as $column) { echo "<td>$column</td>"; } echo "</tr>"; } echo "</table>";
Djsteiner79
commented
Apr 23, 2020
Thank you so much. 😊👍
Djsteiner79
commented
Apr 24, 2020
Hi agaín :)
In the csv file there are two columns ,name and numbers, I will like do use the PHP max() Function to get the highest value in an array of the cummum number?
How can i do that?
There's many ways you can achieve that, I'd suggest reading through this tutorial, or picking up a good book on the PHP language. With a little time, it's easy to pick up the basics.
Djsteiner79
commented
Apr 24, 2020
Thanks I do know some basic PHP and i have made this task, justmore simple, without the csv file, but this is a little more advanced for me :)
<style> table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; }td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
</style>
"; echo "Det lavest Tempartur er: ". (min($temp_array)). "°
"; echo " "; $Mtemp = array( "78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73, 68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73, 45"); $datesOFM = array('01-01-2011, 02-01-2011, 03-01-2011, 04-01-2011, 05-01-2011, 06-01-2011, 07-01-2011, 08-01-2011, 09-01-2011, 10-01-2011,11-01-2011, 12-01-2011, 13-01-2011, 14-01-2011, 15-01-2011,16-01-2011, 17-01-2011, 18-01-2011, 19-01-2011, 20-01-2011, 21-01-2011, 22-01-2011, 23-01-2011, 24-01-2011, 25-01-2011, 26-01-2011, 27-01-2011, 28-01-2011, 29-01-2011, 30-01-2011, 31-01-2011'); echo " "; foreach($Mtemp as $key => $value) { echo "" . ""; } echo "
| Dato | Tempertur |
|---|---|
| " . $datesOFM[$key] . " | " . $Mtemp[$key] . " |
Octagon-simon
commented
Apr 17, 2022
Thanks @rboatright
As of PHP 8.0, you can set the length to 0 (or null, or omit it entirely) for the fgetcsv() function if you don't want to limit the maximum line length.