|
| 1 | +<?php |
| 2 | + |
| 3 | +function arrayToCSV($inputArray) |
| 4 | +{ |
| 5 | + $csvFieldRow = array(); |
| 6 | + foreach ($inputArray as $CSBRow) { |
| 7 | + $csvFieldRow[] = str_putcsv($CSBRow); |
| 8 | + } |
| 9 | + $csvData = implode("\n", $csvFieldRow); |
| 10 | + return $csvData; |
| 11 | +} |
| 12 | + |
| 13 | +function str_putcsv($input, $delimiter = ',', $enclosure = '"') |
| 14 | +{ |
| 15 | + // Open a memory "file" for read/write |
| 16 | + $fp = fopen('php://temp', 'r+'); |
| 17 | + // Write the array to the target file using fputcsv() |
| 18 | + fputcsv($fp, $input, $delimiter, $enclosure); |
| 19 | + // Rewind the file |
| 20 | + rewind($fp); |
| 21 | + // File Read |
| 22 | + $data = fread($fp, 1048576); |
| 23 | + fclose($fp); |
| 24 | + // Ad line break and return the data |
| 25 | + return rtrim($data, "\n"); |
| 26 | +} |
| 27 | + |
| 28 | +$inputArray = array( |
| 29 | + array("First Name", "Last Name", "Identification Number"), |
| 30 | + array("Kim","Thomas","8001"), |
| 31 | + array("Jeffery","Robert","8021"), |
| 32 | + array("Helan","Albert","8705") |
| 33 | +); |
| 34 | +print "<PRE>"; |
| 35 | +print $CSVData = arrayToCSV($inputArray); |
| 36 | +?> |
0 commit comments