Skip to content

Instantly share code, notes, and snippets.

@0x15f
Last active April 11, 2018 13:22
Show Gist options
  • Save 0x15f/5856ef43b8ef23f2cf3877ac484bedb2 to your computer and use it in GitHub Desktop.
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
<?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);
}
@0x15f
Copy link
Author

0x15f commented Apr 11, 2018

I am aware some optimizations could be made.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment