|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* @author Tom Klingenberg <https://github.com/ktomk> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace N98\Util; |
|
7
|
|
|
|
|
8
|
|
|
class ArrayFunctions |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Merge two arrays together. |
|
12
|
|
|
* |
|
13
|
|
|
* If an integer key exists in both arrays, the value from the second array |
|
14
|
|
|
* will be appended the the first array. If both values are arrays, they |
|
15
|
|
|
* are merged together, else the value of the second array overwrites the |
|
16
|
|
|
* one of the first array. |
|
17
|
|
|
* |
|
18
|
|
|
* @see http://packages.zendframework.com/docs/latest/manual/en/index.html#zend-stdlib |
|
19
|
|
|
* @param array $a |
|
20
|
|
|
* @param array $b |
|
21
|
|
|
* @return array |
|
22
|
|
|
*/ |
|
23
|
|
|
public static function mergeArrays(array $a, array $b) |
|
24
|
|
|
{ |
|
25
|
|
|
foreach ($b as $key => $value) { |
|
26
|
|
|
if (array_key_exists($key, $a)) { |
|
27
|
|
|
if (is_int($key)) { |
|
28
|
|
|
$a[] = $value; |
|
29
|
|
|
} elseif (is_array($value) && is_array($a[$key])) { |
|
30
|
|
|
$a[$key] = self::mergeArrays($a[$key], $value); |
|
31
|
|
|
} else { |
|
32
|
|
|
$a[$key] = $value; |
|
33
|
|
|
} |
|
34
|
|
|
} else { |
|
35
|
|
|
$a[$key] = $value; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $a; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param array $matrix |
|
44
|
|
|
* @param string $key key to filter |
|
45
|
|
|
* @param mixed $value to compare against (strict comparison) |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function matrixFilterByValue(array $matrix, $key, $value) |
|
49
|
|
|
{ |
|
50
|
|
|
return self::matrixCallbackFilter($matrix, function (array $item) use ($key, $value) { |
|
51
|
|
|
return $item[$key] !== $value; |
|
52
|
|
|
}); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param array $matrix |
|
57
|
|
|
* @param string $key to filter |
|
58
|
|
|
* @param string $value to compare against |
|
59
|
|
|
* @return array |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function matrixFilterStartswith(array $matrix, $key, $value) |
|
62
|
|
|
{ |
|
63
|
|
|
return self::matrixCallbackFilter($matrix, function (array $item) use ($key, $value) { |
|
64
|
|
|
return strncmp($item[$key], $value, strlen($value)); |
|
65
|
|
|
}); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param array $matrix |
|
70
|
|
|
* @param callable $callback that when return true on the row will unset it |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
|
|
private static function matrixCallbackFilter(array $matrix, $callback) |
|
74
|
|
|
{ |
|
75
|
|
|
foreach ($matrix as $k => $item) { |
|
76
|
|
|
if ($callback($item)) { |
|
77
|
|
|
unset($matrix[$k]); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $matrix; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string[] $columns |
|
86
|
|
|
* @param array $table |
|
87
|
|
|
* @return array table with ordered columns |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function columnOrderArrayTable(array $columns, array $table) |
|
90
|
|
|
{ |
|
91
|
|
|
$closure = function (array $array) use ($columns) { |
|
92
|
|
|
return self::columnOrder($columns, $array); |
|
93
|
|
|
}; |
|
94
|
|
|
|
|
95
|
|
|
if (PHP_VERSION_ID < 50400) { |
|
96
|
|
|
$closure = function (array $array) use ($columns) { |
|
97
|
|
|
return call_user_func(__CLASS__ . '::columnOrder', $columns, $array); |
|
98
|
|
|
}; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return array_map($closure, $table); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* order array entries (named and numbered) of array by the columns given as string keys. |
|
106
|
|
|
* |
|
107
|
|
|
* non-existent columns default to numbered entries or if no numbered entries exists any longer, to null. |
|
108
|
|
|
* |
|
109
|
|
|
* entries in array that could not consume any column are put after the columns. |
|
110
|
|
|
* |
|
111
|
|
|
* @param string[] $columns |
|
112
|
|
|
* @param array $array |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
public static function columnOrder(array $columns, array $array) |
|
116
|
|
|
{ |
|
117
|
|
|
if (!$columns) { |
|
118
|
|
|
return $array; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$keys = array_fill_keys($columns, null); |
|
122
|
|
|
|
|
123
|
|
|
$keyed = array_intersect_key($array, $keys); |
|
124
|
|
|
|
|
125
|
|
|
$arrayLeftover = array_diff_key($array, $keyed); |
|
126
|
|
|
$keysLeftover = array_diff_key($keys, $keyed); |
|
127
|
|
|
|
|
128
|
|
|
$target = array(); |
|
129
|
|
|
if ($keysLeftover) { |
|
130
|
|
|
foreach ($arrayLeftover as $key => $value) { |
|
131
|
|
|
if (is_string($key)) { |
|
132
|
|
|
continue; |
|
133
|
|
|
} |
|
134
|
|
|
$target[key($keysLeftover)] = $value; |
|
135
|
|
|
unset($arrayLeftover[$key]); |
|
136
|
|
|
next($keysLeftover); |
|
137
|
|
|
if (null === key($keysLeftover)) { |
|
138
|
|
|
break; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$result = array_merge($keys, $keyed, $keysLeftover, $target, $arrayLeftover); |
|
144
|
|
|
|
|
145
|
|
|
return $result; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|