1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Botonomous\utility; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class ArrayUtility. |
7
|
|
|
*/ |
8
|
|
|
class ArrayUtility extends AbstractUtility |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Filter an array based on $keepKeys. |
12
|
|
|
* |
13
|
|
|
* @param array $toFilter |
14
|
|
|
* @param array $keepKeys Includes the keys that need to be kept in $toFilter array |
15
|
|
|
* |
16
|
|
|
* @return array |
17
|
|
|
*/ |
18
|
29 |
|
public function filterArray(array $toFilter, array $keepKeys) |
19
|
|
|
{ |
20
|
29 |
|
return array_intersect_key($toFilter, array_flip($keepKeys)); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Check if key is present in $search array and has got a value only if the value is string. |
25
|
|
|
* |
26
|
|
|
* @param $key |
27
|
|
|
* @param array $search |
28
|
|
|
* |
29
|
|
|
* @return bool |
30
|
|
|
*/ |
31
|
31 |
|
public function arrayKeyValueExists(string $key, array $search): bool |
32
|
|
|
{ |
33
|
31 |
|
if (!array_key_exists($key, $search)) { |
34
|
4 |
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
30 |
|
if (is_string($search[$key])) { |
38
|
30 |
|
return strlen(trim($search[$key])) > 0; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Set a value in a nested array based on path. |
46
|
|
|
* |
47
|
|
|
* @param array $array The array to modify |
48
|
|
|
* @param array $path The path in the array |
49
|
|
|
* @param mixed $value The value to set |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
24 |
|
public function setNestedArrayValue(array &$array, array $path, &$value) |
54
|
|
|
{ |
55
|
24 |
|
$current = &$array; |
56
|
24 |
|
foreach ($path as $key) { |
57
|
24 |
|
$current = &$current[$key]; |
58
|
|
|
} |
59
|
|
|
|
60
|
24 |
|
$current = $value; |
61
|
24 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get a value in a nested array based on path |
65
|
|
|
* See https://stackoverflow.com/a/9628276/419887. |
66
|
|
|
* |
67
|
|
|
* @param array $array The array to modify |
68
|
|
|
* @param array $path The path in the array |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
1 |
|
public function getNestedArrayValue(array &$array, array $path) |
73
|
|
|
{ |
74
|
1 |
|
$current = &$array; |
75
|
1 |
|
foreach ($path as $key) { |
76
|
1 |
|
$current = &$current[$key]; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
return $current; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $array |
84
|
|
|
* |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
6 |
|
public function sortArrayByLength(array $array) |
88
|
|
|
{ |
89
|
|
|
usort($array, function ($array1, $array2) { |
90
|
6 |
|
return strlen($array2) <=> strlen($array1); |
91
|
6 |
|
}); |
92
|
|
|
|
93
|
6 |
|
return $array; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Find the key for the max positive value. |
98
|
|
|
* |
99
|
|
|
* @param $array |
100
|
|
|
* |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
4 |
|
public function maxPositiveValueKey(array $array) |
104
|
|
|
{ |
105
|
4 |
|
$maxValue = max($array); |
106
|
|
|
|
107
|
4 |
|
if ($maxValue > 0) { |
108
|
2 |
|
return array_search($maxValue, $array); |
109
|
|
|
} |
110
|
4 |
|
} |
111
|
|
|
} |
112
|
|
|
|