Jump to content

Arrays and str_getcsv


Go to solution Solved by requinix,

Recommended Posts

Hey guys,

 

I haven't posted here in a while, so hello :)

 

So I am confusing myself here and its really frustrating me! I am trying to parse some CSV content thats posted through a textarea input to further manipulate and insert into a database one row at a time.

 

However, my current problem is that there seems to a rolling mismatch which means the output is always one more than the one it should be :S

 

For example:

 

Header 1 - Val1

Header 2 - Val2

Header 3 - Val3

header 4 - Val4 Val1

LINE BREAK

Header 1 - Val2

Header 2 - Val3

Header 3 - Val4

Header 4 - Val1 Val2

LINE BREAK

 

 

I've never experianced this problem before with a foreach loop so I was wondering if someone can tell me where it's going wrong:

<?php

$csvNumColumns = 10;
$csvDelim = ",";
$csvData = $_POST["text"]; 
$data = array_chunk(str_getcsv($csvData, $csvDelim), $csvNumColumns); 

foreach($data as $csvLine){
echo "Header 1: $csvLine[0] <br />"; 
echo "Header 2: $csvLine[1] <br />";
echo "Header 3: $csvLine[2] <br />";
echo "Header 4 - $csvLine[3] <br />";
echo "Header 5: $csvLine[4] <br />";
echo "Header 6: $csvLine[5] <br />";
echo "Header 7: $csvLine[6] <br />";
echo "Header 8: $csvLine[7] <br />";
echo "Header 9: $csvLine[8] <br />";
echo "Header 10:  $csvLine[9] <br />";
echo "<hr />";
}
?>

Any idea what might be causing this?

 

Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/280586-arrays-and-str_getcsv/
Share on other sites

  • Solution

$csvNumColumns = 10;
$csvDelim = ",";
$csvData = $_POST["text"]; 
$data = array_chunk(str_getcsv($csvData, $csvDelim), $csvNumColumns); 
I guess you got that from this user comment on the manual page for str_getcsv? Guess what: it's wrong.

 

I'm posting a comment there right now:

vincent's comment (2010-02-18) will not work.

For multiline array, just use :

<?php
$csvData = file_get_contents($fileName);
$csvNumColumns = 22;
$csvDelim = ";";
$data = array_chunk(str_getcsv($csvData, $csvDelim), $csvNumColumns);
?>

 

Consider the following:

 

<?php

$input =
"r1f1,r1f2,r1f3,r1f4
r2f1,r2f2,r2f3,r2f4
r3f1,r3f2,r3f3,r3f4
r4f1,r4f2,r4f3,r4f4";

print_r(array_chunk(str_getcsv($input), 4));

?>
will output the (paraphrased) array

 

array(
	0 => array(r1f1, r1f2, r1f3, r1f4\nr2f1),
	1 => array(r2f2, r2f3, r2f4\nr3f1, r3f2),
	2 => array(r3f3, r3f4\nr4f1, r4f2, r4f3),
	3 => array(r4f4)
)
which is totally wrong.

 

You MUST split the input into lines. str_getcsv() operates assuming the input is a single line and will thus consider newlines to be part of a value.

 

<?php

$input =
"r1f1,r1f2,r1f3,r1f4
r2f1,r2f2,r2f3,r2f4
r3f1,r3f2,r3f3,r3f4
r4f1,r4f2,r4f3,r4f4";

$csv = array();
foreach (preg_split('/\r\n?|\n/', $input) as $line) {
	$csv[] = str_getcsv($line);
}

?>

 

I cant edit it but I wanted to offer some sample data to help demonstrate:

 

"program_name","merchant_id","code","description","url","start_date","end_date","region","Date_Added","exclusive"

 

"Shop 1","1","CODE1","£20 off £90 spend","http://www.site1.com/ ","2013-07-04 00:00:00","2013-10-31 23:59:59","GB","2013-07-04 01:00:00","No"

 

"Shop 2","2","CODE2","£10 off £60 spend","http://www.site1.com/ ","2013-07-04 00:00:00","2013-10-31 23:59:59","GB","2013-07-04 01:00:00","No"

 

"Shop 3","3","CODE3","£5 off £35 spend","http://www.site1.com/","2013-07-04 00:00:00","2013-10-31 23:59:59","GB","2013-07-04 01:00:00","No"

 

$csvNumColumns = 10;
$csvDelim = ",";
$csvData = $_POST["text"]; 
$data = array_chunk(str_getcsv($csvData, $csvDelim), $csvNumColumns); 
I guess you got that from this user comment on the manual page for str_getcsv? Guess what: it's wrong.

 

I'm posting a comment there right now:

 

That was my exact source! It felt like something that would fit the bill just right.

 

Now how would I encorprate your code within what I've done?

Those four lines I showed? That's the part that needs replacing, but after doing that the code can be cleaned up a bit.

 

Skipping the steps of replacing in the preg_split() stuff I have and doing the cleaning, here's the end result:

<?php

$csvLines = preg_split('/\r\n?|\n/', $_POST["text"]);

foreach ($csvLines as $csvLine) {
	$csvLine = str_getcsv($csvLine);
	// same echos as before
	echo "Header 1: $csvLine[0] <br />"; 
	echo "Header 2: $csvLine[1] <br />";
	echo "Header 3: $csvLine[2] <br />";
	echo "Header 4 - $csvLine[3] <br />";
	echo "Header 5: $csvLine[4] <br />";
	echo "Header 6: $csvLine[5] <br />";
	echo "Header 7: $csvLine[6] <br />";
	echo "Header 8: $csvLine[7] <br />";
	echo "Header 9: $csvLine[8] <br />";
	echo "Header 10:  $csvLine[9] <br />";
	echo "<hr />";
}

?>
Edited by requinix
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.