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

前端 未结 7 1222
梦毁少年i
梦毁少年i 2020-11-29 03:42

here is my csv

column1,column2,column3,column4,column5
column1_row1,column2_row1,column3_row1,column4_row1,column5_row1
column1_row2,column2_row2,column3_row         


        
相关标签:
7条回答
  • 2020-11-29 04:23

    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);
    }
    
    0 讨论(0)
提交回复
热议问题