Created
January 19, 2015 17:28
-
-
Save yratof/a6b5b2746a8d9a85f7af to your computer and use it in GitHub Desktop.
Combine CSV rows, but merge differences
This file contains 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 /* 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>'; |
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
Can you share the CSV which you have taken as a reference?