0

I have a situation where my headers aren't predefined, so I would like to add columns dynamically as I process the input without having to read all the input before writing.

It makes sense to keep track of just the header as an Array and prepend it at the end, but I'm yet to see an easy way to do this.

I've cut my code down to the minimum to understand how it is currently, pretty standard PHP csv writing. I haven't yet added any code to process the dynamic part of it.

$handle = fopen($this->path, 'w');

fputcsv($handle, ['initial', 'header', 'values']);

foreach($data as $datum){
    fputcsv($handle, $datum));
}


Brian
  • 402
  • 3
  • 12
  • Can you show how you currently produce the file and any efforts to process the header. – Nigel Ren Sep 23 '20 at 14:54
  • ok, I've done that. I haven't yet made any effort to process the dynamic bit of the header, since I can't see how at the moment. – Brian Sep 23 '20 at 15:02
  • [How do I prepend file to beginning?](https://stackoverflow.com/questions/3332262/how-do-i-prepend-file-to-beginning) it one part, as you say keep track of all of the fields and then add it in. – Nigel Ren Sep 23 '20 at 15:04
  • You are writing CSV to file or return back to client? I'm confused about fopen – Anton Sep 23 '20 at 15:07
  • @Anton I'm writing CSV to file and later allowing the client to download it. – Brian Sep 23 '20 at 15:09

0 Answers0