Jump to content

Uploading and sorting csv file


Nodral

Recommended Posts

Hi All

 

Please can someone help me.

 

I have a CSV file with over 3000 lines.  Each one contains the following information

 

Country Insurance Name Mandatory or Optional Description and Coverage Group SIPP Cost Per Day Cost Per Week Cost Per Month Deductable

I have written the following script to read line by line, compare the country and insurance with the previous and if it is the same, tag certain parts of each line onto the end of the previous.

 

However, I am getting no output from this, and I keep getting an error saying I am using too much memory or my computer hangs.

 

I am fairly new to php so any pointers would be good.

 

Firstly, does my script look like it should do what I want it to do?

If so, any clues how to get around the momory issue.

If not, what have I done wrong?

 

Cheers guys

 

<?php
//open file paths
$handle=fopen("insurances.csv","r");
$handle_out=fopen("insuranceoutput.csv","a");

//writes the first line of the csv file into an array to act as a comparrison
if($handle){
	$line=fgets($handle);
	$line_array=explode(",",$line);
	$full_array=$line_array;
	fclose($handle);
}

}
if($handle)
{
while (!feof($handle))
{
	// read one line into array
	$line=fgets($handle);
	$line_array=explode(",",$line);
	fclose($handle);

	//test to see if country matches previous
	while ($line_array[0] == $full_array[0])
	{
		//test to see if insurance matches previous
		while ($line_array[1] == $full_array[1])
		{
			//write array values 4 - 9 to main array
			$full_array[]=array("$line_array[4]","$line_array[5]","$line_array[6]","$line_array[7]","$line_array[8]","$line_array[9]");

			}
		}
		//write to new file when new insurance is found

		foreach($full_array as $output)
			{
			fwrite($handle_out, "$output,");
			}
			fwrite($handle_out,"\n");
			fclose($handle_out);

	}
	// write to new file if new country is found.
	foreach($full_array as $output)
			{
			fwrite($handle_out, "$output,");
			}
			fwrite($handle_out,"\n");
			fclose($handle_out);
}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/214615-uploading-and-sorting-csv-file/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.