Sunday, August 28, 2011

Function to Convert CSV to Array using PHP

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


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

No comments:

Post a Comment