public static function arrayToCSV($array, $downloadFile = null)
    {

        if ($downloadFile) {
            header('Content-Type: application/csv');
            header('Content-Disposition: attachement; filename="' . $downloadFile . '"');
        }

        ob_start();
        $f = fopen('php://output', 'w') or ("Can't open php://output");
        $n = 0;
        foreach ($array as $line) {
            $n++;
            if (!fputcsv($f, $line)) {
                ("Can't write line $n: $line");
            }
        }
        fclose($f) or ("Can't close php://output");
        $str = ob_get_contents();
        ob_end_clean();

        if (!$downloadFile) {
            return $str;
        }

        echo $str;
    }
public static function arrayToCSV($array, $downloadFile = null)
    {

        if ($downloadFile) {
            header('Content-Type: application/csv');
            header('Content-Disposition: attachement; filename="' . $downloadFile . '"');
        }

        ob_start();
        $f = fopen('php://output', 'w') or ("Can't open php://output");
        $n = 0;
        foreach ($array as $line) {
            $n++;
            if (!fputcsv($f, $line)) {
                ("Can't write line $n: $line");
            }
        }
        fclose($f) or ("Can't close php://output");
        $str = ob_get_contents();
        ob_end_clean();

        if (!$downloadFile) {
            return $str;
        }

        echo $str;
    }
public static function arrayToCSV ( $ array , $ downloadFile = null ) { if ( $ downloadFile ) { header ( 'Content-Type: application/csv' ) ; header ( 'Content-Disposition: attachement; filename="' . $ downloadFile . '"' ) ; } ob_start ( ) ; $ f = fopen ( 'php://output' , 'w' ) or ( "Can't open php://output" ) ; $ n = 0 ; foreach ( $ array as $ line ) { $ n ++ ; if ( ! fputcsv ( $ f , $ line ) ) { ( "Can't write line $n: $line" ) ; } } fclose ( $ f ) or ( "Can't close php://output" ) ; $ str = ob_get_contents ( ) ; ob_end_clean ( ) ; if ( ! $ downloadFile ) { return $ str ; } echo $ str ; }
Transform an array into a csv

@param $array
@param null $downloadFile if download is set => will output directly the csv
@return string
Transform an array into a csv