Convert CSV to Array function PHP without using str_getcsv()

Tuesday, November 22, 2011

Processing CSV file for web projects is very usefull to show CSV Upload list, Upload data to database table, processing bulk posting etc., Here is the function to process CSV file, using this function we can convert our CSV file to PHP Mufti dimensional Array,  str_getcsv() is not supported under version 5. so let we start create our own CSV to Array Function


Custom CSS to Array in PHP

# Desc: Function to Make PHP Array from CSV file data
# Parm: $csv_path,$header_row,$preserve_keys (usefull for Database Insert)
# Return: Array $output['header'], output['data'];
function csv_to_array($csv_file,$preserve_keys=FALSE,$header_row=TRUE)
 while (!feof($fp)) {
 $data[]=explode(",",fgets($fp, 1024));
 // Get Field Header and Unset field row from data
  // Preserve Keys or Make array Keyas as Numberic
   foreach($data[0] as $head){javascript:void(0)
   foreach($data[0] as $head){
 // Push data into data key;
 //Return array output
 return $output;

Download This Script    Live Demo    Download Script

1 comment:

QualityPoint said...

Thanks for this information.Its very simple to understand.just you can see the reference for php

Post a Comment