Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. <?php
  2. function toCSV(array $data, array $colHeaders = array(), $asString = false) {
  3. $stream = ($asString)
  4. ? fopen("php://temp/maxmemory", "w+")
  5. : fopen("php://output", "w");
  6.  
  7. if (!empty($colHeaders)) {
  8. fputcsv($stream, $colHeaders);
  9. }
  10.  
  11. foreach ($data as $record) {
  12. fputcsv($stream, $record);
  13. }
  14.  
  15. if ($asString) {
  16. rewind($stream);
  17. $returnVal = stream_get_contents($stream);
  18. fclose($stream);
  19. return $returnVal;
  20. }
  21. else {
  22. fclose($stream);
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement