Skip to content

When changing Duplicate Headers the order is mixed up #287

@stevenbuehner

Description

@stevenbuehner

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;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions