Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

How can i convert my 2d array into csv file with adding header so user can upload the file directly.

Here is my array

Array (   
    [0] => Array
        (
            [0] => 25/2/2013
            [1] => 8.45 a.m
            [2] => 9.98
            [3] => 1.23
            [4] => 6.1
        )

    [1] => Array
        (
            [0] => 25/2/2013
            [1] => 8.46 a.m
            [2] => 9.02
            [3] => 1.75
            [4] => 1.75
        )
 )

and i want my output as

Date Time Value1 Value2 Value3 (all header)

25/2/2013 8.45 a.m 9.98 1.23 6.1

25/2/2013 8.46 a.m 9.02 1.75 1.75

share|improve this question
2  
3  
4th homework: php.net/manual/en/function.fputcsv.php –  Mark Baker Jul 19 at 8:14
 
@MarkBaker Nice, I didn't know about that one. –  Hein A. Grønnestad Jul 19 at 8:24

marked as duplicate by Gordon Jul 29 at 6:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 1 down vote accepted
   $header = array("Date","Time","Value1","Value2","Value3");

   $fp = fopen("php://output", "w");
   fputcsv ($fp, $header, "\t");
   foreach($array as $row){
        fputcsv($fp, $row, "\t");
   }
   fclose($fp);

If you want to trigger a download for the client add the following lines at the top.

  header("Content-Type: text/csv");
  header('Content-disposition: attachment;filename=mycoolfile.csv');
share|improve this answer
 
@MarkBaker thanks –  Orangepill Jul 19 at 8:27
 
thanks for the idea, its work with some changes. –  zira Jul 23 at 2:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.