Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i parse a csv file to grab the column names first then the rows that relate to it?

Tags:

php

csv

fgetcsv

here is my csv

column1,column2,column3,column4,column5
column1_row1,column2_row1,column3_row1,column4_row1,column5_row1
column1_row2,column2_row2,column3_row2,column4_row2,column5_row2
column1_row3,column2_row3,column3_row3,column4_row3,column5_row3
column1_row4,column2_row4,column3_row4,column4_row4,column5_row4
column1_row5,column2_row5,column3_row5,column4_row5,column5_row5
column1_row6,column2_row6,column3_row6,column4_row6,column5_row6
column1_row7,column2_row7,column3_row7,column4_row7,column5_row7
column1_row8,column2_row8,column3_row8,column4_row8,column5_row8
column1_row9,column2_row9,column3_row9,column4_row9,column5_row9

first row is the column names of course. i tried fgetcsv() but all that would do is display all rows. rather than what i want. how can i do it?

so if i were to put the data into an array at the end i would be able print out a table format of the data just like its shown in excel.

thanks

this is my sample:

$filename = "upload/sample.csv";
if (($handle = fopen($filename, 'r')) !== FALSE)
{
    while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) 
    {
       print_r($row);
     }
}

this is my output: (i put the $row into a pre so i can show it)

Array
(
    [0] => column1
    [1] => column2
    [2] => column3
    [3] => column4
    [4] => column5
column1_row1
    [5] => column2_row1
    [6] => column3_row1
    [7] => column4_row1
    [8] => column5_row1
column1_row2
    [9] => column2_row2
    [10] => column3_row2
    [11] => column4_row2
    [12] => column5_row2
column1_row3
    [13] => column2_row3
    [14] => column3_row3
    [15] => column4_row3
    [16] => column5_row3
column1_row4
    [17] => column2_row4
    [18] => column3_row4
    [19] => column4_row4
    [20] => column5_row4
column1_row5
    [21] => column2_row5
    [22] => column3_row5
    [23] => column4_row5
    [24] => column5_row5
column1_row6
    [25] => column2_row6
    [26] => column3_row6
    [27] => column4_row6
    [28] => column5_row6
column1_row7
    [29] => column2_row7
    [30] => column3_row7
    [31] => column4_row7
    [32] => column5_row7
column1_row8
    [33] => column2_row8
    [34] => column3_row8
    [35] => column4_row8
    [36] => column5_row8
column1_row9
    [37] => column2_row9
    [38] => column3_row9
    [39] => column4_row9
    [40] => column5_row9
)
like image 253
Exploit Avatar asked Apr 15 '11 08:04

Exploit


3 Answers

For reading it all at once you can use:

$csv = array_map("str_getcsv", file("file1.csv",FILE_SKIP_EMPTY_LINES));
$keys = array_shift($csv);

To turn all the rows into a nice associative array you could then apply:

foreach ($csv as $i=>$row) {
    $csv[$i] = array_combine($keys, $row);
}
like image 119
mario Avatar answered Nov 15 '22 17:11

mario


// Opening the file for reading...
$fp = fopen('path_to_your_file.csv', 'r');

// Headrow
$head = fgetcsv($fp, 4096, ';', '"');

// Rows
while($column = fgetcsv($fp, 4096, ';', '"'))
{
    // This is a great trick, to get an associative row by combining the headrow with the content-rows.
    $column = array_combine($head, $column);

    echo $column['column1'];
}
like image 39
Fidi Avatar answered Nov 15 '22 17:11

Fidi


I made this quick solution using fgetcsv, works fine and it's easier to read and understand. Then the whole result is saved in the $csv array

$csv = array();
$i = 0;
if (($handle = fopen($upload['file'], "r")) !== false) {
    $columns = fgetcsv($handle, 1000, ",");
    while (($row = fgetcsv($handle, 1000, ",")) !== false) {
        $csv[$i] = array_combine($columns, $row);
        $i++;
    }
    fclose($handle);
}
like image 1
Macr1408 Avatar answered Nov 15 '22 17:11

Macr1408