Skip to content

Instantly share code, notes, and snippets.

@icambridge
Created October 27, 2017 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icambridge/dab62265a02a09b8c14244ace113a255 to your computer and use it in GitHub Desktop.
Save icambridge/dab62265a02a09b8c14244ace113a255 to your computer and use it in GitHub Desktop.
<?php
$row = 1;
$fileNo = 1;
$outfp = fopen(__DIR__."/out.csv", "w+");
$files = glob("*.csv");
$originalHeaders = [];
foreach ($files as $file)
{
$row = 0;
if (($handle = fopen($file, "r")) !== FALSE) {
$headers = [];
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$row++;
if ($row == 1) {
$headers = $data;
if ($fileNo == 1) {
$originalHeaders = $headers;
fputcsv($outfp, $originalHeaders);
}
continue;
}
$num = count($data);
$line = array_combine($headers, $data);
$sortedLine = sortToOriginalHeaders($originalHeaders, $line);
fputcsv($outfp, $sortedLine);
}
fclose($handle);
}
$fileNo++;
}
function sortToOriginalHeaders($originalHeaders, $line) {
var_dump($line, $originalHeaders);
$output = [];
foreach ($originalHeaders as $key => $value) {
$output[$key] = $line[$value];
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment