Created
August 4, 2015 14:58
-
-
Save fijiwebdesign/bbd9bf7d291f7fcab29b to your computer and use it in GitHub Desktop.
Parsing a CSV file - sorting and aggregating entries
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 | |
$filename = './Voters_for_SDC4CRetriever_Official-groupby-email.csv'; | |
$name = null; | |
$entry_fp = null; | |
$entry_arr = []; | |
$fp = fopen($filename, 'r'); | |
while (true) { | |
$arr = fgetcsv($fp); | |
if ($arr[1] == 'votes' || !$arr) { | |
$name = $arr[6]; | |
$filename = './entries-ip-sort/' . str_replace(' ', '-', $name) . '.csv'; | |
if ($entry_fp) { | |
$csv_str = ''; | |
$ip_arr = []; | |
$ip_list = []; | |
foreach($entry_arr as $i => $_arr) { | |
$ips = array_map('trim', explode("\n", $_arr[7])); | |
foreach($ips as $_i_ip => $ip) { | |
if (in_array($i . '.' . $_i_ip, $ip_list) ) { | |
continue; | |
} | |
$ip_list[] = $i . '.' . $_i_ip; | |
$ip_entry = $_arr; | |
$ip_entry[7] = $ip; | |
if ($ip !== '') $ip_entry[8] = '- unique IP'; | |
$ip_arr[] = $ip_entry; | |
if ($ip == '') { | |
continue; | |
} | |
foreach($entry_arr as $_i => $__arr) { | |
if ($_i == $i) { | |
continue; | |
} | |
$_ips = array_map('trim', explode("\n", $__arr[7])); | |
if (in_array($ip, $_ips)) { | |
$ip_key = array_search($ip, $_ips); | |
$ip_list[] = $_i . '.' . $ip_key; | |
$ip_entry = $__arr; | |
$ip_entry[7] = $ip; | |
$ip_entry[8] = '* same IP'; | |
$ip_arr[] = $ip_entry; | |
} | |
} | |
} | |
} | |
$sort_ip_arr = []; | |
$last_ip = null; | |
$tmp_sort_arr = []; | |
foreach($ip_arr as $ip_entry) { | |
if ($ip_entry[7] == $last_ip) { | |
$tmp_sort_arr[] = $ip_entry; | |
} else { | |
usort($tmp_sort_arr, function($a, $b) { | |
return strcmp($a[5], $b[5]); | |
}); | |
var_dump($tmp_sort_arr); | |
$sort_ip_arr = array_merge($sort_ip_arr, $tmp_sort_arr); | |
$tmp_sort_arr = []; | |
$last_ip = $ip_entry[7]; | |
} | |
} | |
foreach($sort_ip_arr as $ip_entry) { | |
$csv_str .= '"' . implode('","', $ip_entry) . "\"\n"; | |
} | |
fwrite($entry_fp, $csv_str); | |
fclose($entry_fp); | |
} | |
$entry_fp = fopen($filename, 'w+'); | |
$entry_arr = []; | |
} | |
if ($entry_fp) { | |
$entry_arr[] = $arr; | |
} | |
if (!$arr) { | |
break; | |
} | |
} | |
if (is_resource($fp)) { | |
fclose($fp); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment