forked from ryanfb/pleiades-tgn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alignment.php
67 lines (61 loc) · 1.49 KB
/
alignment.php
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
<?php
/******
* Author: Ethan Gruber
* Date: 23 Oct. 2015
* Function: a PHP script to merge TGN matches from Ryan Baumann's Pleiades-TGN script into Nomisma
*/
//generate arrays
$np = generate_json('nomisma-pleiades.csv');
$pt = generate_json('pleiades-tgn.csv');
$count = 0;
foreach ($np as $row){
$pleiades = $row['match'];
$tgn = '';
foreach ($pt as $pt_row){
if ($pt_row['pleiades'] == $pleiades){
$np[$count]['tgn'] = $pt_row['tgn'];
break;
}
}
$count++;
}
$file = fopen('nomisma-pleiades-tgn.csv', 'w');
fputcsv($file, array('nomisma','pleiades','tgn'));
foreach ($np as $row) {
fputcsv($file, $row);
}
fclose($file);
/**** FUNCTIONS ****/
function generate_json($doc){
$keys = array();
$array = array();
$csv = csvToArray($doc, ',');
// Set number of elements (minus 1 because we shift off the first row)
$count = count($csv) - 1;
//Use first row for names
$labels = array_shift($csv);
foreach ($labels as $label) {
$keys[] = $label;
}
// Bring it all together
for ($j = 0; $j < $count; $j++) {
$d = array_combine($keys, $csv[$j]);
$array[$j] = $d;
}
return $array;
}
// Function to convert CSV into associative array
function csvToArray($file, $delimiter) {
if (($handle = fopen($file, 'r')) !== FALSE) {
$i = 0;
while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) {
for ($j = 0; $j < count($lineArray); $j++) {
$arr[$i][$j] = $lineArray[$j];
}
$i++;
}
fclose($handle);
}
return $arr;
}
?>