Export csv file from array

By Ankit Doshi

I am using below code for export multidimensional array.
I have below problems with my code so any one please look at it or help me out.
I am using codeigniter framework(2.1.0 Version)

Problems :

  1. I have no any double quotes/single quotes in array but in csv file some fields have doble quotes.

  2. Each and every csv file give me first two line blanks then contents i can see

CODE :

function array_to_csv($array, $download = "")
    {
        if ($download != "")
        {
            header('Content-Type: text/csv');
            header('Content-Disposition: attachement; filename="' . $download . '"');
            header("Pragma: no-cache");
            header("Expires: 0");
        }
        ob_start();
        $f = fopen('php://output', 'w') or show_error("Can't open php://output");
        $n = 0;
        foreach ($array as $line)
        {
        $line = str_replace('"', '', $line); //Try to resolve first problem but it doesn't work for me.
            $n++;
            if ( ! fputcsv($f, $line))
            {
                show_error("Can't write line $n: $line");
            }
        }
        fclose($f) or show_error("Can't close php://output");
        $str = ob_get_contents();
        ob_end_clean();
        if ($download == "")
        {
            return $str;
        }
        else
        {
            echo $str;
        }
    }

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273