Friday, 15 April 2016

How to Generate CSV File from PHP Array

In this post you will learn how to generate CSV(Comma-Separated Values) file from PHP Array. Suppose friends if you have data in array and you do not want to enter into mysql table and you want the user directly download that data into csv formate. This things are learn into this video.

Source Code

      // How to Generate CSV File from Array in PHP Script       
      $results = array (  
           "0" => array(  
                "name"           => "Anna Smith",  
                "email_id"      => ""  
           "1" => array(  
                "name"           => "Johnny Huck",  
                "email_id" => ""  
      $filename = 'userData.csv';       
      header("Content-type: text/csv");       
      header("Content-Disposition: attachment; filename=$filename");       
      $output = fopen("php://output", "w");       
      $header = array_keys($results[0]);       
      fputcsv($output, $header);       
      foreach($results as $row)       
           fputcsv($output, $row);  


Post a Comment