-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvdiffs.php
More file actions
121 lines (107 loc) · 2.51 KB
/
csvdiffs.php
File metadata and controls
121 lines (107 loc) · 2.51 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* csvDiffs.php finds and returns arrays of differences between two .csv files
* version 0.1 finds all unique rows and rows where one column matches but any other columm differs
* requires PHP >=5.3
*
* Released under the GNU/LGPL licences -- David Collins -- June, 2012
*
* You may freely use, modify or redistribute this script provided this header remains intact
*
* @title csvDiffs.php
* @author David Collins <collidavid@gmail.com>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS:0.1 $Id:$
* @link https://github.com/davidcollins/csvDiffs
*/
/**
* define files to be handled if not set elsewhere
*/
if(!isset($file1)){
$file1="";
};
if(!isset($file2)){
$file2="";
};
/**
* set an error message for no file selected
*/
$no_file_error="no file selected";
/**
* put CSV files into single-diminesion json encoded array
* to compare it as a string
*/
function fcsv2Ajsn($f){
global $no_file_error;
if(file_exists($f)){
$f = fopen($f, 'r');
}
else {
die($no_file_error);
};
while(($line = fgets($f)) !== FALSE){
$arr[]= json_encode($line);
};
fclose($f);
return $arr;
} ;
/**
* make array of all rows in file 1 where #col not in file 2
*/
function diffsCol($arr1, $arr2, $col){
if($col!="0"){
$col--;
};
$vc=array();
$uc=array();
foreach($arr1 as $v){
$vc[] = $v[$col];
$arr3[$v[$col]]=$v;
};
foreach($arr2 as $u){
$uc[] = $u[$col];
};
$d = array_diff($vc, $uc);
$d = array_flip($d);
$d = array_intersect_key($arr3,$d);
return $d;
};
/**
* find all rows in file 1 where col in file 2 the same but other cols changed:
* works best if needle column has unique values
* decrement $col to synch array[$col] with csv column count
*/
function diffsUCol($arr1, $arr2, $col ){
if ($col!="0"){$col--;};
$dif=array();
foreach($arr1 as $val)
{
foreach($arr2 as $v)
{
if($val[$col]==$v[$col]){
$tdif=array();
$tdif[] = array_merge($val,$v);
$dif[] = $tdif;
};
};
};
return $dif;
};
$indx1 = fcsv2Ajsn($file1);
$indx2 = fcsv2Ajsn($file2);
/**
* get all rows in file1 that don't have matching rows in file2
* go through it both ways to find unique rows in each file.
*/
$difRs = array_diff($indx1,$indx2);
$difRs1 = array_diff($indx2,$indx1);
/**
* find unique rows where selected columns match in each file
*/
foreach($difRs as $val){
$difR[]=str_getcsv(json_decode($val));
};
foreach($difRs1 as $val){
$difR1[]=str_getcsv(json_decode($val));
};
?>