data-import icon indicating copy to clipboard operation
data-import copied to clipboard

When changing Duplicate Headers the order is mixed up

Open stevenbuehner opened this issue 9 years ago • 1 comments

When using the CSVReader with the option

        $this->setHeaderRowNumber ( 0, CsvReader::DUPLICATE_HEADERS_INCREMENT );

then the column-name does not fit to the result anymore...

Here is my suggestion to solve the problem:

    /**
     * Overriding default function
     *
     * @param array $headers            
     *
     * @return array
     */
        protected function incrementHeaders(array $headers) {
        $incrementedHeaders = array();

        // Get all headlines that are duplicate or more
        foreach ( array_count_values ( $headers ) as $header => $count ) {
            if ($count > 1) {
                $incrementedHeaders [$header] = 0;
            }
        }

        // Replace the headers with the new header name but keep the position ($key) in the array
        foreach ( $headers as $key => $headerName ) {
            if (isset ( $incrementedHeaders [$headerName] )) {
                $prefix = empty ( $headerName ) ? 'UNKNOWN' : '';
                $headers [$key] = $prefix . $headerName . $incrementedHeaders [$headerName] ++;
            }
        }

        return $headers;
    }

stevenbuehner avatar Feb 21 '16 21:02 stevenbuehner

@stevenbuehner - why not create a pull request with your changes? Might be better

kyleobrien91 avatar Jun 07 '16 01:06 kyleobrien91