3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = array ( array ( 'UserID' => '5GDPR6LR', 'BatchID' => '1', 'BatchItemID' => '1', 'Categories' => '["1","2"]', 'UserComment' => 'first comment', ) , array ( 'UserID' => '5GDPR6LR', 'BatchID' => '1', 'BatchItemID' => '2', 'Categories' => '["3","4"]', 'UserComment' => 'second comment', ) , array ( 'UserID' => '5GDPR6LR', 'BatchID' => '1', 'BatchItemID' => '3', 'Categories' => '["5","6"]', 'UserComment' => 'third comment', ) , ); $headers = array_keys($data[0]); echo '<pre>ARRAY TO WRITE TO FILE:<br>'; print_r($data); echo '</pre><hr>'; $responseFile = '/tmp/12'; $fp = fopen($responseFile, 'w'); fputcsv($fp, $headers, ",", "\"", "\\", "\n"); $lastrow = array(); foreach ($data as $row) { echo '<pre>LASTROW:'; print_r($lastrow); echo '</pre>'; echo '<pre>THISROW:'; print_r($row); echo '</pre>'; if ($row !== $lastrow) { fputcsv($fp, $row, ",", "\"", "\\", "\n"); echo '<pre>ROW WRITTEN TO CSV:'; print_r($row); echo '</pre>'; } $lastrow = $row; } fclose($fp); echo "\n".file_get_contents('/tmp/12') ?>

preferences:
45.64 ms | 409 KiB | 5 Q