note 105814 added to function.fputcsv
| From: | MangeshSangapu at osu1 dot php dot net | Date: | Sat, 17 Sep 2011 00:15:21 +0000 |
| Subject: | note 105814 added to function.fputcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-183229@lists.php.net to get a copy of this message | ||
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!!
----
Server IP: 69.147.83.197
Probable Submitter: 198.205.17.200
----
Manual Page -- http://www.php.net/manual/en/function.fputcsv.php
Edit -- https://master.php.net/note/edit/105814
Del: integrated -- https://master.php.net/note/delete/105814/integrated
Del: useless -- https://master.php.net/note/delete/105814/useless
Del: bad code -- https://master.php.net/note/delete/105814/bad+code
Del: spam -- https://master.php.net/note/delete/105814/spam
Del: non-english -- https://master.php.net/note/delete/105814/non-english
Del: in docs -- https://master.php.net/note/delete/105814/in+docs
Del: other reasons-- https://master.php.net/note/delete/105814
Reject -- https://master.php.net/note/reject/105814
Search -- https://master.php.net/manage/user-notes.php