Edit: New answer.
So there's a somewhat convoluted solution that does this:
- Takes your CSV data
- Combine the numeric key data with ALL available headers, giving you a key => value array
- Intersect this with your ALLOWED headers
- Add this to the final data array
- Removes the header row
There we go. It's awful to read, but it seems to be fine. As all the array manipulation is C based, it should be faster than manipulating thousands of rows in PHP.
$availableHeaders = array('NAME', 'THING', 'EMAIL');
$headers = array('NAME', 'EMAIL');
$theData = array();
if (($handle = fopen($uploadedFile, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$theData[] = array_intersect_key(array_combine(array_values($availableHeaders), array_values($data)), array_flip($headers));
}
fclose($handle);
}
array_shift($theData); // Remove headers