Guest User

Untitled

a guest
Nov 19th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. <?php
  2. /**
  3. * @param object $obj ( may be array )
  4. * @return mixed string or null
  5. */
  6. function csv_encode($obj)
  7. {
  8. $csv = null;
  9. $rows = is_object($obj) ? json_decode(json_encode($obj), true) : $obj;
  10.  
  11. if ( $rh = fopen('php://memory', 'w') ) {
  12. fputcsv($rh, array_keys($rows[0]));
  13.  
  14. foreach ( $rows as $row ) {
  15. fputcsv($rh, array_values($row));
  16. }
  17.  
  18. rewind($rh);
  19. $csv = stream_get_contents($rh);
  20. fclose($rh);
  21. }
  22.  
  23. return $csv;
  24. }
Add Comment
Please, Sign In to add comment