1 | <?php |
||
3 | class HelperArray { |
||
4 | |||
5 | /** |
||
6 | * Overwrites old array with new |
||
7 | */ |
||
8 | const MERGE_OVERWRITE = 0; |
||
9 | /** |
||
10 | * Merges old array with new with array_merge() |
||
11 | * String keys replaced, numeric keys renumbered |
||
12 | */ |
||
13 | const MERGE_PHP = 1; |
||
14 | |||
15 | |||
16 | /** |
||
17 | * No array cloning - just stay with same objects |
||
18 | */ |
||
19 | const CLONE_ARRAY_NONE = 0; |
||
20 | /** |
||
21 | * Clone objects on first level of array |
||
22 | */ |
||
23 | const CLONE_ARRAY_SHALLOW = 1; |
||
24 | /** |
||
25 | * Clone objects recursive on any array level |
||
26 | */ |
||
27 | const CLONE_ARRAY_RECURSIVE = 2; |
||
28 | |||
29 | /** |
||
30 | * Convert $delimiter delimited string to array |
||
31 | * |
||
32 | * @param mixed $string |
||
33 | * @param string $delimiter |
||
34 | * |
||
35 | * @return array |
||
36 | */ |
||
37 | 6 | public static function stringToArray($string, $delimiter = ',') { |
|
40 | |||
41 | /** |
||
42 | * Convert single value to array by reference |
||
43 | * |
||
44 | * @param mixed &$value |
||
45 | */ |
||
46 | 3 | public static function makeArrayRef(&$value, $index = 0) { |
|
51 | |||
52 | |||
53 | /** |
||
54 | * Convert single value to array |
||
55 | * |
||
56 | * @param mixed $value |
||
57 | * |
||
58 | * @return array |
||
59 | */ |
||
60 | 3 | public static function makeArray($value, $index = 0) { |
|
65 | |||
66 | /** |
||
67 | * Filters array by callback |
||
68 | * |
||
69 | * @param mixed $array |
||
70 | * @param callable $callback |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | 15 | public static function filter(&$array, $callback) { |
|
87 | |||
88 | /** |
||
89 | * Filter empty() values from array |
||
90 | * |
||
91 | * @param $array |
||
92 | * |
||
93 | * @return array |
||
94 | */ |
||
95 | 8 | public static function filterEmpty($array) { |
|
98 | |||
99 | /** |
||
100 | * @param string $string |
||
101 | * @param string $delimiter |
||
102 | * |
||
103 | * @return array |
||
104 | */ |
||
105 | 1 | public static function stringToArrayFilterEmpty($string, $delimiter = ',') { |
|
108 | |||
109 | /** |
||
110 | * @param mixed|array &$arrayOld |
||
111 | * @param mixed|array $arrayNew |
||
112 | * @param int $mergeStrategy - default is HelperArray::MERGE_OVERWRITE |
||
113 | */ |
||
114 | 1 | public static function merge(&$arrayOld, $arrayNew = array(), $mergeStrategy = HelperArray::MERGE_OVERWRITE) { |
|
128 | |||
129 | /** |
||
130 | * Checks if key exists in array. If yes - return value on key otherwise return default value |
||
131 | * |
||
132 | * @param array &$array |
||
133 | * @param mixed $key |
||
134 | * @param mixed $default |
||
135 | * |
||
136 | * @return mixed |
||
137 | */ |
||
138 | 1 | public static function keyExistsOr(&$array, $key, $default) { |
|
141 | |||
142 | /** |
||
143 | * Clone objects in array |
||
144 | * |
||
145 | * @param array &$array - Any dimensional array with presumed objects in there |
||
146 | * @param int $deep - HelperArray::CLONE_ARRAY_xxx constants |
||
147 | */ |
||
148 | 3 | public static function cloneDeep(&$array, $deep = HelperArray::CLONE_ARRAY_RECURSIVE) { |
|
161 | |||
162 | /** |
||
163 | * Repacking array to provided level, removing null elements |
||
164 | * |
||
165 | * @param array &$array |
||
166 | * @param int $level |
||
167 | */ |
||
168 | public static function array_repack(&$array, $level = 0) { |
||
185 | |||
186 | } |
||
187 |