-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget_zips.php
More file actions
69 lines (56 loc) · 2.09 KB
/
get_zips.php
File metadata and controls
69 lines (56 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
//increase memory limit assigned to php just in case
ini_set('memory_limit','1256M');
//changed to read file names from prompt instead of in the same line as the command.
$st_file = readline("Enter the name of the SailThru file: ");
$sfg_file = readline("Enter the name of the other file: ");
//This section just turns the csv into arrays with keys being the csv headers
$st_csv = array_map("str_getcsv", file(trim($st_file),FILE_SKIP_EMPTY_LINES));
$sfg_csv = array_map("str_getcsv", file(trim($sfg_file),FILE_SKIP_EMPTY_LINES));
$st_keys = array_shift($st_csv);
$sfg_keys = array_shift($sfg_csv);
foreach ($st_csv as $r=>$row) {
$r = trim(strtolower($r));
$st_csv[$r] = array_combine($st_keys, $row);
}
foreach ($sfg_csv as $l=>$line) {
$l = trim(strtolower($l));
$sfg_csv[$l] = array_combine($sfg_keys, $line);
}
//end code for turning csv files into key and value associative arrays
//two arrays to hold values
$st_array = array();
$sfg_array = array();
//iterates through the arrays made above to just get the associative array
for($st = 0;$st < count($st_csv); $st++) {
$st_array[$st_csv[$st]["Email"]] = $st_csv[$st]["us_zips"];
}
for($sfg = 0;$sfg < count($sfg_csv); $sfg++) {
$sfg_array[$sfg_csv[$sfg]["E-mail Address"]] = $sfg_csv[$sfg]["ZIP/Postal Code"];
}
//end conversion of array to proper associative array
//main array to hold email to zip association
$zips_array = array();
//loop through SailThru's array to find matching emails in SFG files, then put them into the zips array
foreach($st_array as $email => $zip) {
if(array_key_exists($email, $sfg_array) && $sfg_array[$email] !== null) {
$zips_array[$email] = $sfg_array[$email];
}
}
//create final file
$zips_file = fopen("zips_updated.csv", "w+");
//headers for the final csv file
$headers[0] = "Email";
$headers[1] = "zip_code";
//write headers to final csv file
fputcsv($zips_file, $headers);
//loop through the zips array and write email/zip pairings
foreach($zips_array as $email => $zip) {
$temp_arr[0] = $email;
$temp_arr[1] = $zip;
//output to the file
fputcsv($zips_file, $temp_arr);
}
//close file
fclose($zips_file);
?>