Created
October 27, 2017 13:18
-
-
Save icambridge/dab62265a02a09b8c14244ace113a255 to your computer and use it in GitHub Desktop.
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 | |
$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