src/Csv.php
public function unparse($data = array(), $fields = array(), $append = FileProcessingModeEnum::MODE_FILE_OVERWRITE, $is_php = false, $delimiter = null) {
        if (!is_array($data) || empty($data)) {
            $data = &$this->data;
        } else {
            /** @noinspection ReferenceMismatchInspection */
            $this->data = $data;
        }

        if (!is_array($fields) || empty($fields)) {
            $fields = &$this->titles;
        }

        if ($delimiter === null) {
            $delimiter = $this->delimiter;
        }

        $string = $is_php ? "<?php header('Status: 403'); die(' '); ?>" . $this->linefeed : '';
        $entry = array();

        // create heading
        /** @noinspection ReferenceMismatchInspection */
        $fieldOrder = $this->_validate_fields_for_unparse($fields);
        if (!$fieldOrder && !empty($data)) {
            $column_count = count($data[0]);
            $columns = range(0, $column_count - 1, 1);
            $fieldOrder = array_combine($columns, $columns);
        }

        if ($this->heading && !$append && !empty($fields)) {
            foreach ($fieldOrder as $column_name) {
                $entry[] = $this->_enclose_value($column_name, $delimiter);
            }

            $string .= implode($delimiter, $entry) . $this->linefeed;
            $entry = array();
        }

        // create data
        foreach ($data as $key => $row) {
            foreach (array_keys($fieldOrder) as $index) {
                $cell_value = $row[$index];
                $entry[] = $this->_enclose_value($cell_value, $delimiter);
            }

            $string .= implode($delimiter, $entry) . $this->linefeed;
            $entry = array();
        }

        if ($this->convert_encoding) {
            $string = iconv($this->input_encoding, $this->output_encoding, $string);
        }

        return $string;
    }
public function unparse($data = array(), $fields = array(), $append = FileProcessingModeEnum::MODE_FILE_OVERWRITE, $is_php = false, $delimiter = null) {
        if (!is_array($data) || empty($data)) {
            $data = &$this->data;
        } else {
            /** @noinspection ReferenceMismatchInspection */
            $this->data = $data;
        }

        if (!is_array($fields) || empty($fields)) {
            $fields = &$this->titles;
        }

        if ($delimiter === null) {
            $delimiter = $this->delimiter;
        }

        $string = $is_php ? "<?php header('Status: 403'); die(' '); ?>" . $this->linefeed : '';
        $entry = array();

        // create heading
        /** @noinspection ReferenceMismatchInspection */
        $fieldOrder = $this->_validate_fields_for_unparse($fields);
        if (!$fieldOrder && !empty($data)) {
            $column_count = count($data[0]);
            $columns = range(0, $column_count - 1, 1);
            $fieldOrder = array_combine($columns, $columns);
        }

        if ($this->heading && !$append && !empty($fields)) {
            foreach ($fieldOrder as $column_name) {
                $entry[] = $this->_enclose_value($column_name, $delimiter);
            }

            $string .= implode($delimiter, $entry) . $this->linefeed;
            $entry = array();
        }

        // create data
        foreach ($data as $key => $row) {
            foreach (array_keys($fieldOrder) as $index) {
                $cell_value = $row[$index];
                $entry[] = $this->_enclose_value($cell_value, $delimiter);
            }

            $string .= implode($delimiter, $entry) . $this->linefeed;
            $entry = array();
        }

        if ($this->convert_encoding) {
            $string = iconv($this->input_encoding, $this->output_encoding, $string);
        }

        return $string;
    }
public function unparse ( $ data = array ( ) , $ fields = array ( ) , $ append = FileProcessingModeEnum :: MODE_FILE_OVERWRITE , $ is_php = false , $ delimiter = null ) { if ( ! is_array ( $ data ) || empty ( $ data ) ) { $ data = & $ this -> data ; } else { /** @noinspection ReferenceMismatchInspection */ $ this -> data = $ data ; } if ( ! is_array ( $ fields ) || empty ( $ fields ) ) { $ fields = & $ this -> titles ; } if ( $ delimiter === null ) { $ delimiter = $ this -> delimiter ; } $ string = $ is_php ? "<?php header('Status: 403'); die(' '); ?>" . $ this -> linefeed : '' ; $ entry = array ( ) ; // create heading /** @noinspection ReferenceMismatchInspection */ $ fieldOrder = $ this -> _validate_fields_for_unparse ( $ fields ) ; if ( ! $ fieldOrder && ! empty ( $ data ) ) { $ column_count = count ( $ data [ 0 ] ) ; $ columns = range ( 0 , $ column_count - 1 , 1 ) ; $ fieldOrder = array_combine ( $ columns , $ columns ) ; } if ( $ this -> heading && ! $ append && ! empty ( $ fields ) ) { foreach ( $ fieldOrder as $ column_name ) { $ entry [ ] = $ this -> _enclose_value ( $ column_name , $ delimiter ) ; } $ string .= implode ( $ delimiter , $ entry ) . $ this -> linefeed ; $ entry = array ( ) ; } // create data foreach ( $ data as $ key => $ row ) { foreach ( array_keys ( $ fieldOrder ) as $ index ) { $ cell_value = $ row [ $ index ] ; $ entry [ ] = $ this -> _enclose_value ( $ cell_value , $ delimiter ) ; } $ string .= implode ( $ delimiter , $ entry ) . $ this -> linefeed ; $ entry = array ( ) ; } if ( $ this -> convert_encoding ) { $ string = iconv ( $ this -> input_encoding , $ this -> output_encoding , $ string ) ; } return $ string ; }
Create CSV data string from array

@param array[]     $data       2D array with data
@param array       $fields     field names
@param bool        $append     if true, field names will not be output
@param bool        $is_php     if a php die() call should be put on the
first line of the file, this is later
ignored when read.
@param string|null $delimiter  field delimiter to use

@return string CSV data
Create CSV data string from array