Last active
April 11, 2018 13:22
-
-
Save 0x15f/5856ef43b8ef23f2cf3877ac484bedb2 to your computer and use it in GitHub Desktop.
Simple PHP Script allows you to handle uploaded CSV files w/ empty columns/headers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$file = file($_FILES['file-name']['tmp_name']); | |
$csv_rows = array_map('str_getcsv', file($_FILES['customer-csv']['tmp_name'])); | |
$csv_header = array_filter(array_shift($csv_rows)); | |
$merged = []; | |
foreach($csv_rows as $row) { | |
$row = array_filter($row); | |
while(count($row) !== count($csv_header)) { | |
if(count($row) > count($csv_header)) { | |
unset($row[count($row) - 1]); | |
} | |
else { | |
$row[count($row) + 1] = null; | |
} | |
} | |
$merged[] = array_combine($csv_header, $row); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am aware some optimizations could be made.