Creating arrays from csv columns in PHP -
i've never worked csv files before , i'm hoping create arrays in php columns.
so example csv this:
name, address, dob
john doe, 22 grove street, 06,06,2006
i'd each of columns array named column headings. time parse file(test.csv) arrays of table rows instead.
following code work you. , can see real working script of exactcode @ url:
http://sugunan.net/demo/csv1.php
$i=0; $filename = 'data1.csv'; $contents = file($filename); foreach($contents $line) { $line = preg_replace( "/\r|\n/", "", $line ); $line_array = explode(",",$line); if($i==0) { foreach($line_array $key=>$val) { $csv_heading[$key] = trim($val); } } else { foreach($line_array $key=>$val) { $csv_array[$i][$csv_heading[$key]] = $val; } } $i++; } print_r($csv_array);
data file url: http://sugunan.net/demo/data1.csv
Comments
Post a Comment