Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @param object $obj ( may be array )
- * @return mixed string or null
- */
- function csv_encode($obj)
- {
- $csv = null;
- $rows = is_object($obj) ? json_decode(json_encode($obj), true) : $obj;
- if ( $rh = fopen('php://memory', 'w') ) {
- fputcsv($rh, array_keys($rows[0]));
- foreach ( $rows as $row ) {
- fputcsv($rh, array_values($row));
- }
- rewind($rh);
- $csv = stream_get_contents($rh);
- fclose($rh);
- }
- return $csv;
- }
Add Comment
Please, Sign In to add comment