2

I am trying to write a csv files with values from two arrays. Here's the code:

<?php
fopen('output.csv','w');
$merged_fields = array_combine($authors, $ids);

$range = count($merged_fields);//gives number of elements in array
$name = array_keys($merged_fields);
$id = array_values($merged_fields);

$name = str_replace(',',', ',$name);

for ($a = 0; $a <=$range; $a++) {
    //filter out rows where id = '000000'
    if($id[$a] == '000000'){
    continue;
    }
fputcsv($fp, array_filter(array($name[$a], $id[$a])));
  }
}
fclose($fp);    
?>

If the original array was "Smith, John" => "888888", "Smith, Jane"=>"777777" , and the next array is "Jones,John"=>"999999" there is a space after the line after each array, so in the csv the output ends up:

"Smith, John",888888
"Smith, Jane",777777

"Johnes, John",999999

I will use this csv to import data and need to get the extra lines out of there. I have tried applying "array_filter" to other parts of the array as it appears there is an empty element in there somewhere, but that hasn't worked.

5
  • I'm not certain but I think this might be expected behavior. fputcsv will end with a new line character as per the docs, so taking that into consideration, it seems to be expected since each loop contains the fputcsv function. Commented Dec 16, 2015 at 21:52
  • Can you add the code where you open the file? There is a mistake in the loop (you should get an error): in the for loop condition $a <= $range should be $a < $range. Also the variable $id a bit further is undefined. Should it be $empid? Commented Dec 16, 2015 at 22:13
  • @trincot -- I cleaned up the variables, I was changing them for clarity and left a couple. They were all correct when I ran my code. I added an fopen statement. I do get a "notice" for an undefined value based in the $a variable when it gets beyond the range of the array. Commented Dec 16, 2015 at 22:39
  • When you speak of the "next array "Jones,John"=>"999999"", does that mean you call this piece of code more than once with the intention to add to the file? Commented Dec 16, 2015 at 22:44
  • yes, it's in a foreach loop. I am parsing RIS files. Each record is for an academic journal article. Each instance of $authors contains all of the authors associated with each journal article. Commented Dec 17, 2015 at 1:39

1 Answer 1

2

The blank line is produced because you iterate one time too many over the array. You will have a few warning messages like these:

E_NOTICE :  type 8 -- Undefined offset: ...

But the code will not be interrupted by that and so an empty value will be output by fputcsv, resulting in the empty line. If then you call the same code again to append to the generated file, the empty line will separate the two batches of output.

Fix this by replacing <= in the following line:

for ($a = 0; $a <= $range; $a++) {

so you get:

for ($a = 0; $a < $range; $a++) {

Remember that if an array has $range elements, the first element is at index 0 and the last at index $range-1, unless you have explicitly defined your array otherwise.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.