Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When changing Duplicate Headers the order is mixed up #287

Open
stevenbuehner opened this issue Feb 21, 2016 · 1 comment
Open

When changing Duplicate Headers the order is mixed up #287

stevenbuehner opened this issue Feb 21, 2016 · 1 comment

Comments

@stevenbuehner
Copy link

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;
    }
@kyleobrien91
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants