1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Utilities; |
9
|
|
|
|
10
|
|
|
abstract class ArrayUtilities |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param array $array |
14
|
|
|
* |
15
|
|
|
* @return bool |
16
|
|
|
*/ |
17
|
9 |
|
public static function is_multidimensional(array &$array) |
|
|
|
|
18
|
|
|
{ |
19
|
9 |
|
foreach ($array as &$element) |
20
|
|
|
{ |
21
|
9 |
|
if (is_array($element)) |
22
|
|
|
{ |
23
|
9 |
|
return true; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
6 |
|
return false; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Check whether the elements of a multidimensional array can be indexed based on a key. |
32
|
|
|
* |
33
|
|
|
* In the following example, the array can be indexed with the 'name' key. |
34
|
|
|
* |
35
|
|
|
* ```php |
36
|
|
|
* array( |
37
|
|
|
* array('name' => 'sample 1'), |
38
|
|
|
* array('name' => 'sample 2'), |
39
|
|
|
* array('name' => 'sample 3'), |
40
|
|
|
* ) |
41
|
|
|
* ``` |
42
|
|
|
* |
43
|
|
|
* @param array $arr |
44
|
|
|
* @param string $indexKey The key to consider the index |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
44 |
|
public static function array_can_be_indexed(array &$arr, $indexKey) |
|
|
|
|
49
|
|
|
{ |
50
|
44 |
|
foreach ($arr as &$value) |
51
|
|
|
{ |
52
|
44 |
|
if (isset($value[$indexKey]) && is_scalar($value[$indexKey])) |
53
|
|
|
{ |
54
|
44 |
|
return true; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
40 |
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Index an array passed on given key. |
63
|
|
|
* |
64
|
|
|
* The resulting array would be as follows when indexed by the 'name' key. |
65
|
|
|
* |
66
|
|
|
* ```php |
67
|
|
|
* array( |
68
|
|
|
* 'sample 1' => array('name' => 'sample 1'), |
69
|
|
|
* 'sample 2' => array('name' => 'sample 2'), |
70
|
|
|
* 'sample 3' => array('name' => 'sample 3'), |
71
|
|
|
* ) |
72
|
|
|
* ``` |
73
|
|
|
* |
74
|
|
|
* @param array $arr |
75
|
|
|
* @param string $indexKey |
76
|
|
|
* |
77
|
|
|
* @see ArrayUtilities::array_can_be_indexed() |
78
|
|
|
* |
79
|
|
|
* @return array The original array but each element's key will be the value of the element's key value |
80
|
|
|
*/ |
81
|
4 |
|
public static function array_index_by_key(array &$arr, $indexKey) |
|
|
|
|
82
|
|
|
{ |
83
|
4 |
|
$result = array(); |
84
|
|
|
|
85
|
4 |
|
foreach ($arr as &$value) |
86
|
|
|
{ |
87
|
4 |
|
if (isset($value[$indexKey]) && is_scalar($value[$indexKey])) |
88
|
|
|
{ |
89
|
4 |
|
$result[$value[$indexKey]] = $value; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
4 |
|
return $result; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Merge two arrays together while respecting possible indexing. |
98
|
|
|
* |
99
|
|
|
* @param array $arr1 The first array |
100
|
|
|
* @param array $arr2 The values in this array will overwrite the respective values from $arr2 |
101
|
|
|
* @param string $indexKey The key to respect when indexing |
102
|
|
|
* |
103
|
|
|
* @see ArrayUtilities::array_index_by_key() |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
53 |
|
public static function array_merge_defaults(array &$arr1, array &$arr2, $indexKey) |
|
|
|
|
108
|
|
|
{ |
109
|
53 |
|
$merged = $arr1; |
110
|
|
|
|
111
|
53 |
|
foreach ($arr2 as $key => &$value) |
112
|
|
|
{ |
113
|
39 |
|
if (is_array($value) && isset($arr1[$key])) |
114
|
|
|
{ |
115
|
38 |
|
if (self::array_can_be_indexed($value, $indexKey)) |
116
|
|
|
{ |
117
|
1 |
|
$indexedArr1 = self::array_index_by_key($arr1[$key], $indexKey); |
118
|
1 |
|
$indexedArr2 = self::array_index_by_key($value, $indexKey); |
119
|
1 |
|
$merged[$key] = array_merge($indexedArr1, $indexedArr2); |
120
|
|
|
} |
121
|
|
|
else |
122
|
|
|
{ |
123
|
37 |
|
$merged[$key] = array_merge($arr1[$key], $value); |
124
|
|
|
} |
125
|
|
|
|
126
|
38 |
|
continue; |
127
|
|
|
} |
128
|
|
|
|
129
|
38 |
|
$merged[$key] = $value; |
130
|
|
|
} |
131
|
|
|
|
132
|
53 |
|
return $merged; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Split an associative array into two chunks based on the position of a key. |
137
|
|
|
* |
138
|
|
|
* @param string $key The key we'll be using as the splitting point |
139
|
|
|
* @param array $array The array we're splitting up |
140
|
|
|
* @param bool $inclusive When set to true, the key being used as the splitting point will be incorporated in the |
141
|
|
|
* first chunk of the split. |
142
|
|
|
* |
143
|
|
|
* @return array An array with two indices (0 & 1) containing the respective chunks of the array |
144
|
|
|
*/ |
145
|
9 |
|
public static function associative_array_split($key, array &$array, $inclusive = true) |
|
|
|
|
146
|
|
|
{ |
147
|
9 |
|
$offset = array_search($key, array_keys($array)) + (int)$inclusive; |
148
|
9 |
|
$result = array(); |
149
|
|
|
|
150
|
9 |
|
$result[0] = array_slice($array, 0, $offset, true); |
151
|
9 |
|
$result[1] = array_slice($array, $offset, null, true); |
152
|
|
|
|
153
|
9 |
|
return $result; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
This check looks for method names that are not written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes
databaseConnectionSeeker
.