note 76895 deleted from function.fputcsv by levim
| From: | levim@php.net | Date: | Wed, 07 Aug 2013 16:23:29 +0000 |
| Subject: | note 76895 deleted from function.fputcsv by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195899@lists.php.net to get a copy of this message | ||
Note Submitter: Jonathon Hibbard at phpingenuity.com
----
This function is a replacement for PHP 4. It basically takes an array you pass it, and creates a
delimited string. If you want ot use the key as the field name, you can tell the second paramater
to use the key. The last is the delimiter you want each value to be seperated by. Should be pretty
straight forward.
<?php
function generate_csv_data($data,$use_key=false,$delm=',') {
$output = NULL;
if(is_array($data)) {
if($use_key == false) {
if(isset($data[0]) && is_array($data[0])) {
foreach($data as $key) {
$output .= implode($delm,$key);
$output .= "\n";
}
} else {
$output .= implode("$delm", $data)."\n";
}
} else {
foreach($data as $key => $value) {
$output .= "$key{$delm}$value\n";
}
}
} else {
$output = $data;
}
if(empty($output)) {
trigger_error('OUTPUT WAS EMPTY!', E_USER_ERROR);
return false;
}
return $output;
}
?>