Process CSV Into Array With Column Headings For Key

后端 未结 9 2516
星月不相逢
星月不相逢 2020-11-29 03:35

I have a CSV with the first row containing the field names. Example data is...

\"Make\",\"Model\",\"Note\"
\"Chevy\",\"1500\",\"loaded\"
\"Chevy\",\"2500\",         


        
9条回答
  •  被撕碎了的回忆
    2020-11-29 04:19

    $csv_data = array_map('str_getcsv', file('Book.csv'));// reads the csv file in php array
    $csv_header = $csv_data[0];//creates a copy of csv header array
    unset($csv_data[0]);//removes the header from $csv_data since no longer needed
    foreach($csv_data as $row){
        $row = array_combine($csv_header, $row);// adds header to each row as key
        var_dump($row);//do something here with each row
    }
    

提交回复
热议问题