Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 59525

Fixed problem – Add variable delimiter for export to csv file – to import excel

$
0
0

Replies: 1

Hi guys!
Following a problem with a complex data import, I made a change to the export-csv.php file to add the possibility to customize the delimiter in the csv file output. I think it can be useful to many!

open the file “export-csv.php” located in the “\inc” folder of the plugin and change the array2csv function as follows


    public function array2csv(array &$array, $df){
        $delimiter = '|';
        if (count($array) == 0) {
            return null;
        }

        $array_keys = array_keys($array);
        $heading    = array();
        $unwanted   = array('cfdb7_', 'your-');

        foreach ( $array_keys as $aKeys ) {
            $tmp       = str_replace( $unwanted, '', $aKeys );
            $heading[] = ucfirst( $tmp );
        }
        fputcsv( $df, $heading, $delimiter);

        foreach ( $array['form_id'] as $line => $form_id ) {
            $line_values = array();
            foreach($array_keys as $array_key ) {
                $val = isset( $array[ $array_key ][ $line ] ) ? $array[ $array_key ][ $line ] : '';
                $line_values[ $array_key ] = $val;
            }
            fputcsv($df, $line_values, $delimiter);
        }
    }

Viewing all articles
Browse latest Browse all 59525

Trending Articles