note 105814 deleted from function.fputcsv by danbrown
| From: | danbrown@php.net | Date: | Sun, 18 Sep 2011 01:47:57 +0000 |
| Subject: | note 105814 deleted from function.fputcsv by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183244@lists.php.net to get a copy of this message | ||
Note Submitter: Mangesh Sangapu
----
here's one way to include the header row, if you have a zero-indexed, multi-dimensional array
like:
Array
(
[0] => Array
(
[firstname] => bruce
[lastname] => willis
)
[1] => Array
(
[firstname] => mel
[lastname] => gibson
)
)
/************************************/
$dataArray = your multi-dimensional array
$keyArray = array of keys from the $dataArray
$fp = fopen('output.csv', 'w');
$keyArray[0] = array_keys($dataArray[0]);
foreach ($keyArray as $key)
{
fputcsv($fp, $key);
}
the output will be: firstname, lastname
if you use this in addition to:
foreach ($dataArray as $fields)
{
fputcsv($fp, $fields);
}
then you will have a complete .CSV with headers!!