Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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
You can’t perform that action at this time.