note 88353 added to function.str-getcsv
| From: | Jeremy at osu1 dot php dot net | Date: | Wed, 21 Jan 2009 01:20:46 +0000 |
| Subject: | note 88353 added to function.str-getcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-149570@lists.php.net to get a copy of this message | ||
After using several methods in the past to create CSV strings without using files (disk IO sucks), I
finally decided it's time to write a function to handle it all. This function could use some
cleanup, and the variable type test might be overkill for what is needed, I haven't thought
about it too much.
Also, I took the liberty of replacing fields with certain data types with strings which I find much
easier to work with. Some of you may not agree with those. Also, please note that the type
"double" or float has been coded specifically for two digit precision because if I am
using a float, it's most likely for currency.
I am sure some of you out there would appreciate this function.
<?php
function str_putcsv($array, $delimiter = ',', $enclosure = '"',
$terminator = "\n") {
# First convert associative array to numeric indexed array
foreach ($array as $key => $value) $workArray[] = $value;
$returnString = ''; # Initialize return string
$arraySize = count($workArray); # Get size of array
for ($i=0; $i<$arraySize; $i++) {
# Nested array, process nest item
if (is_array($workArray[$i])) {
$returnString .= str_putcsv($workArray[$i], $delimiter, $enclosure, $terminator);
} else {
switch (gettype($workArray[$i])) {
# Manually set some strings
case "NULL": $_spFormat = ''; break;
case "boolean": $_spFormat = ($workArray[$i] == true) ? 'true':
'false'; break;
# Make sure sprintf has a good datatype to work with
case "integer": $_spFormat = '%i'; break;
case "double": $_spFormat = '%0.2f'; break;
case "string": $_spFormat = '%s'; break;
# Unknown or invalid items for a csv - note: the datatype of array is already handled above,
assuming the data is nested
case "object":
case "resource":
default: $_spFormat = ''; break;
}
$returnString .= sprintf('%2$s'.$_spFormat.'%2$s', $workArray[$i],
$enclosure) . ($i < ($arraySize-1)) ? $delimiter : $terminator;
}
}
# Done the workload, return the output information
return $returnString;
}
?>
----
Server IP: 216.235.13.26
Probable Submitter: 24.87.135.74
----
Manual Page -- http://www.php.net/manual/en/function.str-getcsv.php
Edit -- https://master.php.net/note/edit/88353
Del: integrated -- https://master.php.net/note/delete/88353/integrated
Del: useless -- https://master.php.net/note/delete/88353/useless
Del: bad code -- https://master.php.net/note/delete/88353/bad+code
Del: spam -- https://master.php.net/note/delete/88353/spam
Del: non-english -- https://master.php.net/note/delete/88353/non-english
Del: in docs -- https://master.php.net/note/delete/88353/in+docs
Del: other reasons-- https://master.php.net/note/delete/88353
Reject -- https://master.php.net/note/reject/88353
Search -- https://master.php.net/manage/user-notes.php