-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayHelper.php
More file actions
93 lines (84 loc) · 2.52 KB
/
ArrayHelper.php
File metadata and controls
93 lines (84 loc) · 2.52 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
<?php
namespace Intersvyaz\HttpSession;
class ArrayHelper
{
/**
* Recursive diff between arrays.
* @param array $a
* @param array $b
* @return array
*/
public static function arrayRecursiveDiff($a, $b)
{
$result = [];
foreach ($a as $key => $value) {
if (array_key_exists($key, $b)) {
if (is_array($value)) {
$aRecursiveDiff = self::arrayRecursiveDiff($value, $b[$key]);
if (count($aRecursiveDiff)) {
$result[$key] = $aRecursiveDiff;
}
} else {
if (is_object($value) || is_object($b[$key])) {
if ($value != $b[$key]) {
$result[$key] = $value;
}
} elseif ($value !== $b[$key]) {
$result[$key] = $value;
}
}
} else {
$result[$key] = $value;
}
}
return $result;
}
/**
* Deletes keys from array A that are in array B but not in array C.
* @param array $a
* @param array $b
* @param array $c
* @return array
*/
public static function arrayRemovedRecursiveDiff($a, $b, $c)
{
$result = $a;
foreach ($b as $key => $value) {
if (is_array($value)) {
if (array_key_exists($key, $result)) {
$result[$key] = self::arrayRemovedRecursiveDiff($result[$key], $value, $c[$key]);
}
} elseif (!array_key_exists($key, $c) && is_array($result)) {
unset($result[$key]);
}
}
return $result;
}
/**
* Merge array from Yii2.
* @param array $a
* @param array $b
* @return array
*/
public static function merge($a, $b)
{
$args = func_get_args();
$res = array_shift($args);
while (!empty($args)) {
foreach (array_shift($args) as $k => $v) {
if (is_int($k)) {
if (array_key_exists($k, $res)) {
$res[] = $v;
} else {
$res[$k] = $v;
}
} elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) {
$res[$k] = self::merge($res[$k], $v);
} else {
$res[$k] = $v;
}
}
}
return $res;
}
}