/
marray.php
128 lines (107 loc) · 3.84 KB
/
marray.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
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
122
123
124
125
126
127
128
<?php
namespace Gajus\Marray;
/**
* Strip-down $input to values where $input key is found among $template values.
*
* @throws Gajus\Marray\Exception\InvalidArgumentException If input is not an associative array.
* @throws Gajus\Marray\Exception\InvalidArgumentException If template is not a list.
* @throws Gajus\Marray\Exception\InvalidArgumentException If $input does not have all the keys defined in $template.
* @param array $input
* @param array $template
* @return array
*/
function template (array $input, array $template) {
if (is_int(key($input))) {
// Naive, though misuse cases are just as naive.
throw new Exception\InvalidArgumentException('Input is not an associative array.');
}
if (!is_int(key($template))) {
// Naive, though misuse cases are just as naive.
throw new Exception\InvalidArgumentException('Template is not a list.');
}
$template = array_flip($template);
if ($diff = array_diff_key($template, $input)) {
throw new Exception\InvalidArgumentException('Template does not cover input.');
}
return array_intersect_key($input, $template);
}
/**
* http://php.net/array_intersect recursive implementation.
*
* @param array $arr1 The array with master values to check.
* @param array $arr2 An array to compare values against.
* @param array ... A variable list of arrays to compare.
* @return array
*/
function intersect_recursive (array $arr1, array $arr2) {
$return = [];
$common_keys = array_intersect(array_keys($arr1), array_keys($arr2));
foreach ($common_keys as $key) {
if (is_array($arr1[$key]) && is_array($arr2[$key])) {
$intersection = intersect_recursive ($arr1[$key], $arr2[$key]);
if ($intersection) {
$return[$key] = $intersection;
}
} else if ($arr1[$key] == $arr2[$key]) {
$return[$key] = $arr1[$key];
}
}
if (func_num_args() > 2) {
$arguments = func_get_args();
array_splice($arguments, 0, 2, [$return]);
$return = call_user_func_array('Gajus\Marray\intersect_recursive', $arguments);
}
return $return;
}
/**
* http://php.net/array_diff_key recursive implementation.
*
* @todo Support variadic input.
* @param array $arr1 The array with master keys to check.
* @param array $arr2 An array to compare keys against.
* @return array
*/
function diff_key_recursive (array $arr1, array $arr2) {
$diff = array_diff_key($arr1, $arr2);
$intersect = array_intersect_key($arr1, $arr2);
foreach ($intersect as $k => $v) {
if (is_array($arr1[$k]) && is_array($arr2[$k])) {
$d = diff_key_recursive($arr1[$k], $arr2[$k]);
if ($d) {
$diff[$k] = $d;
}
}
}
return $diff;
}
/**
* http://php.net/array_unique implementation with user callback.
*
* @param array The input array.
* @param callable $value_func Function must return the value used for comparison.
* @param int $sort_flags
* @return array
*/
function uunique ($array, callable $value_func, $sort_flags = \SORT_STRING) {
$copy = array_unique(array_map($value_func, $array), $sort_flags);
return array_intersect_key($array, $copy);
}
/**
* http://uk1.php.net/array_walk_recursive implementation that is used to remove nodes from the array.
*
* @param array The input array.
* @param callable $callback Function must return boolean value indicating whether to remove the node.
* @return array
*/
function walk_recursive_remove (array $array, callable $callback) {
foreach ($array as $k => $v) {
if (is_array($v)) {
$array[$k] = walk_recursive_remove($v, $callback);
} else {
if ($callback($v, $k)) {
unset($array[$k]);
}
}
}
return $array;
}