1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jasny; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Get items from an array. |
7
|
|
|
* Set default values using [key => value]. |
8
|
|
|
* |
9
|
|
|
* <code> |
10
|
|
|
* list($foo, $bar, $useAll) = extract_keys($_GET, ['foo', 'bar', 'all' => false]); |
11
|
|
|
* </cody> |
12
|
|
|
* |
13
|
|
|
* @param array $array |
14
|
|
|
* @param array $keys |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
|
|
function extract_keys(array $array, array $keys) |
|
|
|
|
18
|
|
|
{ |
19
|
1 |
|
$values = []; |
20
|
|
|
|
21
|
1 |
|
foreach ($keys as $i => $v) { |
22
|
1 |
|
$key = is_int($i) ? $v : $i; |
23
|
1 |
|
$default = is_int($i) ? null : $v; |
24
|
|
|
|
25
|
1 |
|
$values[] = isset($array[$key]) ? $array[$key] : $default; |
26
|
1 |
|
} |
27
|
|
|
|
28
|
1 |
|
return $values; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Walk through the array and unset an item with the key |
33
|
|
|
* |
34
|
|
|
* @param array $array Array with objects or arrays |
35
|
|
|
* @param string|array $key |
36
|
|
|
*/ |
37
|
|
|
function array_unset(array &$array, $key) |
|
|
|
|
38
|
|
|
{ |
39
|
2 |
|
foreach ($array as &$item) { |
40
|
2 |
|
if (is_object($item)) { |
41
|
1 |
|
foreach ((array)$key as $k) { |
42
|
1 |
|
if (isset($item->$k)) unset($item->$k); |
|
|
|
|
43
|
1 |
|
} |
44
|
2 |
|
} elseif (is_array($item)) { |
45
|
1 |
|
foreach ((array)$key as $k) { |
46
|
1 |
|
if (isset($item[$k])) unset($item[$k]); |
|
|
|
|
47
|
1 |
|
} |
48
|
1 |
|
} |
49
|
2 |
|
} |
50
|
2 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return an array with only the specified keys. |
54
|
|
|
* |
55
|
|
|
* @param array $array |
56
|
|
|
* @param array $keys |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
function array_only(array $array, array $keys) |
|
|
|
|
60
|
|
|
{ |
61
|
1 |
|
$intersect = array_fill_keys($keys, null); |
62
|
1 |
|
return array_intersect_key($array, $intersect); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Return an array without the specified keys. |
67
|
|
|
* |
68
|
|
|
* @param array $array |
69
|
|
|
* @param array $keys |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
function array_without(array $array, array $keys) |
|
|
|
|
73
|
|
|
{ |
74
|
1 |
|
$intersect = array_fill_keys($keys, null); |
75
|
1 |
|
return array_diff_key($array, $intersect); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Check if an array contains a set of values. |
80
|
|
|
* |
81
|
|
|
* @param array $array |
82
|
|
|
* @param array $subset |
83
|
|
|
* @param boolean $strict Strict type checking |
84
|
|
|
* @return boolean |
85
|
|
|
*/ |
86
|
|
|
function array_contains(array $array, array $subset, $strict = false) |
|
|
|
|
87
|
|
|
{ |
88
|
3 |
|
foreach ($subset as $value) { |
89
|
3 |
|
if (!in_array($value, $array, $strict)) { |
90
|
3 |
|
return false; |
91
|
|
|
} |
92
|
3 |
|
} |
93
|
|
|
|
94
|
3 |
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Check if an array contains a set of values with index check. |
99
|
|
|
* |
100
|
|
|
* @param array $array |
101
|
|
|
* @param array $subset |
102
|
|
|
* @param boolean $strict Strict type checking |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
|
|
function array_contains_assoc(array $array, array $subset, $strict = false) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
foreach ($subset as $key => $value) { |
108
|
|
|
if ( |
109
|
|
|
!array_key_exists($key, $array) || |
110
|
|
|
isset($value) !== isset($array[$key]) || |
111
|
|
|
($strict ? $value !== $array[$key] : $value != $array[$key]) |
112
|
|
|
) { |
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return true; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Flatten a nested associative array, concatenating the keys. |
122
|
|
|
* |
123
|
|
|
* @param array $array |
124
|
|
|
* @param string $glue |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
function array_flatten(array $array, $glue = '.') |
|
|
|
|
128
|
|
|
{ |
129
|
3 |
|
foreach ($array as $key => &$value) { |
130
|
3 |
|
if (!is_associative_array($value)) { |
131
|
3 |
|
continue; |
132
|
|
|
} |
133
|
|
|
|
134
|
3 |
|
unset($array[$key]); |
135
|
3 |
|
$value = array_flatten($value, $glue); |
136
|
|
|
|
137
|
3 |
|
foreach ($value as $subkey => $subvalue) { |
138
|
3 |
|
$array[$key . $glue . $subkey] = $subvalue; |
139
|
3 |
|
} |
140
|
3 |
|
} |
141
|
|
|
|
142
|
3 |
|
return $array; |
143
|
|
|
} |
144
|
|
|
|
This check looks for functions that have already been defined in other files.
Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the
@ignore
annotation.See also the PhpDoc documentation for @ignore.