# jc-importer/2.7.7/class/Common/Exporter/File/CSVFile.php

Import WP – CSV &amp; XML Import Export for WordPress, version 2.7.7. 48 lines.

- Page: https://pluginprobe.com/plugins/jc-importer/2.7.7/code/class/Common/Exporter/File/CSVFile.php
- Raw: https://pluginprobe.com/plugins/jc-importer/2.7.7/raw/class/Common/Exporter/File/CSVFile.php
- Modified: 2023-02-17T21:40:42+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/jc-importer/2.7.7/code/class/Common/Exporter/File/CSVFile.php#L10-L20`.

```php
<?php

namespace ImportWP\Common\Exporter\File;

use ImportWP\Common\Exporter\Mapper\MapperData;

class CSVFile extends File
{
    private $columns;
    public function start()
    {
        $fields = $this->exporter->getFields();

        $this->columns = array_reduce($fields, function ($carry, $item) {
            $carry[$this->getFieldLabel($item)] = isset($item['selection']) ? $item['selection'] : '';
            return $carry;
        }, []);
        // write headers
        fputcsv($this->fh, array_keys($this->columns));
    }

    /**
     * @param MapperData $mapper
     * @return void
     */
    public function add($mapper)
    {
        $data = [];
        $data = array_map(function ($item) use ($mapper) {
            $record = $mapper->data([]);
            $tmp = $mapper->get_value($item, $record[0]);
            return is_array($tmp) ? implode(',', $tmp) : $tmp;
        }, $this->columns);

        fputcsv($this->fh, $data);
    }

    public function end()
    {
        fclose($this->fh);
    }

    public function get_file_name()
    {
        return $this->exporter->getId() . '.csv';
    }
}

```
