-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDifference_Class.php
89 lines (79 loc) · 2.98 KB
/
Difference_Class.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/*
Compare two devices arrays
Returns array of differences btw main array and other array
*/
class Difference {
// public $differences = array();
function getDifferencesV1($mainDevice, $device) {
$differences = array();
foreach($device as $key => $value) {
// key from $device exists in $mainDevice
if (isset($mainDevice[$key])) {
// if value is array, reuse function on it
if (is_array($value) && (!empty($value))) {
$differences[$key] = $this->getDifferencesV1($mainDevice[$key], $value);
// values do not match = its a diff
} elseif ($value != $mainDevice[$key]) {
$differences[$key] = $value;
}
// key doesnt exists in $mainDevice array = its a diff
} else {
$differences[$key] = $value;
}
}
return $differences;
}
///////////////////////////////// IGNORE BELOW ////////////////////////////
/*
function isKeyPresent($key, $value, $device) {
// matches array key defined in arrayBis
if (isset($device[$key])) {
echo "$key - $value, <br>";
// if val is array, reuse function on it
if (is_array($value) && (!empty($value))) {
echo "array and not empty : ";
var_dump($this->differences);
$this->differences[$key] = $this->getDifferences($value, $device[$key]);
// keys values are not equal = its a diff
} elseif ($value != $device[$key]) {
$this->differences[$key] = $value;
echo "value different : ";
var_dump($this->differences);
}
// key doesnt exists in both arrays = its a diff
} else {
$this->differences[$key] = $value;
}
}
function getDifferences($mainDevice, $device) {
foreach($mainDevice as $key => $value) {
// matches array key defined in arrayBis
$this->isKeyPresent($key, $value, $device);
}
} */
//////////////
/* function getDifferencesV2($mainDevice, $device) {
foreach($mainDevice as $key => $value) {
if (isset($device[$key])) {
$this->valueCheck($key, $value, $device);
} else {
$this->addDiff($key, $value);
}
}
}
function valueCheck($key, $value, $device) {
if (is_array($value) && (!empty($value))) {
// this line does not work whyyyyyyyyyyyyyyy
echo " values before fail : $key - $value, <br>";
$this->differences[$key] = $this->getDifferencesV2($value, $device[$key]);
} elseif ($value != $device[$key]) {
echo " works well if not array, values : $key - $value, <br>";
$this->addDiff($key, $value);
}
}
function addDiff($key, $value) {
$this->differences[$key] = $value;
} */
}
?>