Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make CSV File form array in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 238
    Comment on it

    Hello reader! If you have an array with the records and you want it to be download as CSV format. PHP has built in functions for making the format of CSV. Lets take the example below:-

    header("Content-Type: text/csv");
    header("Content-Disposition: attachment; filename=file.csv");
    // Disable caching
    header("Cache-Control: no-cache, no-store, must-revalidate"); // HTTP 1.1
    header("Pragma: no-cache"); // HTTP 1.0
    header("Expires: 0"); // Proxies
    
    function outputCSV($ArrrayData) {
        $output = fopen("php://output", "w");
        foreach ($ArrrayData as $count) {
            fputcsv($csv, $count); 
        }
        fclose($csv);
    }
    
    csvCSV(array(
        array("match 2nd", "score 123", "Over 25"),
        array("match 3rd", "score 252", "Over 26"),
        array("match 4th", "score 158", "Over 50")
    ));
    

    On execution of above code you'll receive CSV

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: