Skip to content

Instantly share code, notes, and snippets.

@yratof
Created January 19, 2015 17:28
Show Gist options
  • Save yratof/a6b5b2746a8d9a85f7af to your computer and use it in GitHub Desktop.
Save yratof/a6b5b2746a8d9a85f7af to your computer and use it in GitHub Desktop.
Combine CSV rows, but merge differences
<?php /* This changes the CSV into another CSV. But merges duplicated fields. */
$handle = fopen("CLUBEXP.csv", "r");
$row = 0;
$ClubNumberList = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// ID
$ClubNumber = $data[0];
// Day
$day = $data[19];
// Times
$start = $data[20];
$end = $data[21];
// Type of Group
$type = $data[18].' - ';
$ClubTDays = $type . $day.': '.$start.' - '.$end;
if ( array_key_exists($ClubNumber, $ClubNumberList) ) {
$ClubNumberList[$ClubNumber][] = $ClubTDays;
} else {
$ClubNumberList[$ClubNumber] = array($ClubTDays);
}
$row++;
}
if (count($row) > 0) {
$handle = fopen('CLUBEXP_merged.CSV', 'w');
if (!$handle) throw new Exception("Can't open CSV file for merged data");
foreach($ClubNumberList as $ClubNumber => $vals) {
array_unshift($vals, $ClubNumber);
if (!fputcsv($handle, $vals)) throw new Exception("Can't write data to merged CSV file");
}
fclose($handle);
}
echo '<pre>';
print_r( $ClubNumberList );
echo '</pre>';
@yratof
Copy link
Author

yratof commented Feb 27, 2023

Can you share the CSV which you have taken as a reference?

This was 9 years ago, absolutely no idea.

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