Horde_Data_Csv::exportData PHP Метод

exportData() публичный Метод

Builds a CSV file from a given data structure and returns it as a string.
public exportData ( array $data, boolean $header = false, $export_mapping = [] ) : string
$data array A two-dimensional array containing the data set.
$header boolean If true, the rows of $data are associative arrays with field names as their keys.
Результат string The CSV data.
    public function exportData($data, $header = false, $export_mapping = array())
    {
        if (!is_array($data) || count($data) == 0) {
            return '';
        }
        $export = '';
        $eol = "\n";
        $head = array_keys(current($data));
        if ($header) {
            foreach ($head as $key) {
                if (!empty($key)) {
                    if (isset($export_mapping[$key])) {
                        $key = $export_mapping[$key];
                    }
                    $export .= '"' . str_replace('"', '\\"', $key) . '"';
                }
                $export .= ',';
            }
            $export = substr($export, 0, -1) . $eol;
        }
        foreach ($data as $row) {
            foreach ($head as $key) {
                $cell = $row[$key];
                if (!empty($cell) || $cell === 0) {
                    $export .= '"' . str_replace('"', '\\"', $cell) . '"';
                }
                $export .= ',';
            }
            $export = substr($export, 0, -1) . $eol;
        }
        return $export;
    }

Usage Example

Пример #1
0
 public function exportData($data, $header = true, $export_mapping = array())
 {
     return parent::exportData($this->_map($data), $header, $export_mapping);
 }