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

前端 未结 7 1223
梦毁少年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:00

    Can't you just do one fgetcsv call first to get the headers, and then start a loop to get the rest?

    Modified the example found in the manual. It should write out a table with headers and values following.

    ';
    
        // Get headers
        if (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
        {
            echo ''.implode('', $data).'';
        }
    
        // Get the rest
        while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
        {
            echo ''.implode('', $data).'';
        }
        fclose($handle);
        echo '';
    }
    ?>
    

提交回复
热议问题