1
- <?php
1
+ <?php declare (strict_types= 1 );
2
2
3
3
/**
4
4
* Helper class for processing arrays
5
5
*
6
6
* Вспомогательный класс для обработки массивов
7
7
*
8
8
* @author Leonid Sheikman (leonid74)
9
- * @copyright 2019-2022 Leonid Sheikman
9
+ * @copyright 2019-2024 Leonid Sheikman
10
+ *
10
11
* @see https://github.com/Leonid74/helpers-php
11
12
*
12
13
* This file is part of the project.
@@ -42,18 +43,18 @@ public static function arrayKeyExists(
42
43
): bool {
43
44
$ result = false ;
44
45
45
- if ( !empty ( $ mNeedle ) && !empty ( $ aHaystack ) ) {
46
- foreach ( $ aHaystack as $ key => $ mItem ) {
47
- $ result = $ bCaseInsensitive ? \mb_strtolower ( $ mNeedle ) == \mb_strtolower ( $ key ) : (string ) $ mNeedle == (string ) $ key ;
46
+ if (!empty ($ mNeedle ) && !empty ($ aHaystack) ) {
47
+ foreach ($ aHaystack as $ key => $ mItem ) {
48
+ $ result = $ bCaseInsensitive ? \mb_strtolower ($ mNeedle ) == \mb_strtolower ($ key ) : (string ) $ mNeedle == (string ) $ key ;
48
49
49
- if ( $ result ) {
50
+ if ($ result ) {
50
51
break ;
51
52
}
52
53
53
- if ( $ bProcessSubarrays && \is_array ( $ mItem ) ) {
54
- $ result = self ::arrayKeyExists ( $ mNeedle , $ mItem , $ bCaseInsensitive , $ bProcessSubarrays );
54
+ if ($ bProcessSubarrays && \is_array ($ mItem) ) {
55
+ $ result = self ::arrayKeyExists ($ mNeedle , $ mItem , $ bCaseInsensitive , $ bProcessSubarrays );
55
56
56
- if ( $ result ) {
57
+ if ($ result ) {
57
58
break ;
58
59
}
59
60
}
@@ -85,22 +86,22 @@ public static function arrayValueExists(
85
86
): bool {
86
87
$ result = false ;
87
88
88
- if ( !empty ( $ mNeedle ) && !empty ( $ aHaystack ) ) {
89
- foreach ( $ aHaystack as $ mItem ) {
90
- if ( \is_array ( $ mItem ) ) {
91
- if ( !$ bProcessSubarrays ) {
89
+ if (!empty ($ mNeedle ) && !empty ($ aHaystack) ) {
90
+ foreach ($ aHaystack as $ mItem ) {
91
+ if (\is_array ($ mItem) ) {
92
+ if (!$ bProcessSubarrays ) {
92
93
continue ;
93
94
}
94
- $ result = self ::arrayValueExists ( $ mNeedle , $ mItem , $ bCaseInsensitive , $ bProcessSubarrays , $ bStrictTypes );
95
+ $ result = self ::arrayValueExists ($ mNeedle , $ mItem , $ bCaseInsensitive , $ bProcessSubarrays , $ bStrictTypes );
95
96
} else {
96
- if ( $ bCaseInsensitive ) {
97
- $ result = $ bStrictTypes ? ( \gettype ( $ mNeedle ) == \gettype ( $ mItem ) && \mb_strtolower ( $ mNeedle ) == \mb_strtolower ( $ mItem ) ) : \mb_strtolower ( $ mNeedle ) == \mb_strtolower ( $ mItem );
97
+ if ($ bCaseInsensitive ) {
98
+ $ result = $ bStrictTypes ? (\gettype ($ mNeedle ) == \gettype ($ mItem ) && \mb_strtolower ($ mNeedle ) == \mb_strtolower ($ mItem) ) : \mb_strtolower ($ mNeedle ) == \mb_strtolower ($ mItem );
98
99
} else {
99
100
$ result = $ bStrictTypes ? $ mNeedle === $ mItem : (string ) $ mNeedle == (string ) $ mItem ;
100
101
}
101
102
}
102
103
103
- if ( $ result ) {
104
+ if ($ result ) {
104
105
break ;
105
106
}
106
107
}
@@ -116,7 +117,7 @@ public static function arrayValueExists(
116
117
*
117
118
* @param string $sHaystack
118
119
* @param array $aNeedles
119
- * @param int $iOffset (optional, defaults to 0)
120
+ * @param int $iOffset (optional, defaults to 0)
120
121
* @param bool $bCaseInSensitive (optional, defaults to false)
121
122
*
122
123
* @return bool
@@ -127,9 +128,9 @@ public static function arrayNeedlesExists(
127
128
?int $ iOffset = 0 ,
128
129
?bool $ bCaseInSensitive = false
129
130
): bool {
130
- if ( !empty ( $ aNeedles ) && !empty ( $ sHaystack ) ) {
131
- foreach ( $ aNeedles as $ needle ) {
132
- if ( ( $ bCaseInSensitive ? \stripos ( $ sHaystack , $ needle , $ iOffset ) : \strpos ( $ sHaystack , $ needle , $ iOffset ) ) !== false ) {
131
+ if (!empty ($ aNeedles ) && !empty ($ sHaystack) ) {
132
+ foreach ($ aNeedles as $ needle ) {
133
+ if (( $ bCaseInSensitive ? \stripos ($ sHaystack , $ needle , $ iOffset ) : \strpos ($ sHaystack , $ needle , $ iOffset) ) !== false ) {
133
134
return true ;
134
135
}
135
136
}
@@ -162,26 +163,27 @@ public static function arraySearch(
162
163
) {
163
164
$ result = false ;
164
165
165
- if ( !empty ( $ mNeedle ) && !empty ( $ aHaystack ) ) {
166
- foreach ( $ aHaystack as $ key => $ mItem ) {
167
- if ( \is_array ( $ mItem ) ) {
168
- if ( !$ bProcessSubarrays ) {
166
+ if (!empty ($ mNeedle ) && !empty ($ aHaystack) ) {
167
+ foreach ($ aHaystack as $ key => $ mItem ) {
168
+ if (\is_array ($ mItem) ) {
169
+ if (!$ bProcessSubarrays ) {
169
170
continue ;
170
171
}
171
- $ result = self ::arraySearch ( $ mNeedle , $ mItem , $ bCaseInsensitive , $ bProcessSubarrays , $ bStrictTypes );
172
+ $ result = self ::arraySearch ($ mNeedle , $ mItem , $ bCaseInsensitive , $ bProcessSubarrays , $ bStrictTypes );
172
173
173
- if ( false !== $ result ) {
174
+ if (false !== $ result ) {
174
175
break ;
175
176
}
176
177
} else {
177
- if ( $ bCaseInsensitive ) {
178
- $ result = $ bStrictTypes ? ( \gettype ( $ mNeedle ) == \gettype ( $ mItem ) && \mb_strtolower ( $ mNeedle ) == \mb_strtolower ( $ mItem ) ) : \mb_strtolower ( $ mNeedle ) == \mb_strtolower ( $ mItem );
178
+ if ($ bCaseInsensitive ) {
179
+ $ result = $ bStrictTypes ? (\gettype ($ mNeedle ) == \gettype ($ mItem ) && \mb_strtolower ($ mNeedle ) == \mb_strtolower ($ mItem) ) : \mb_strtolower ($ mNeedle ) == \mb_strtolower ($ mItem );
179
180
} else {
180
181
$ result = $ bStrictTypes ? $ mNeedle === $ mItem : (string ) $ mNeedle == (string ) $ mItem ;
181
182
}
182
183
183
- if ( false !== $ result ) {
184
+ if (false !== $ result ) {
184
185
$ result = $ key ;
186
+
185
187
break ;
186
188
}
187
189
}
@@ -200,7 +202,7 @@ public static function arraySearch(
200
202
*
201
203
* @param mixed $mNeedle
202
204
* @param array $aHaystack
203
- * @param bool $bCaseInsensitive (optional, defaults to false)
205
+ * @param bool $bCaseInsensitive (optional, defaults to false)
204
206
* @param bool $bProcessSubarrays (optional, defaults to true)
205
207
*
206
208
* @return mixed
@@ -213,20 +215,21 @@ public static function arrayStrPos(
213
215
) {
214
216
$ result = false ;
215
217
216
- if ( !empty ( $ mNeedle ) && !empty ( $ aHaystack ) ) {
217
- foreach ( $ aHaystack as $ key => $ mItem ) {
218
- if ( \is_array ( $ mItem ) ) {
219
- if ( !$ bProcessSubarrays ) {
218
+ if (!empty ($ mNeedle ) && !empty ($ aHaystack) ) {
219
+ foreach ($ aHaystack as $ key => $ mItem ) {
220
+ if (\is_array ($ mItem) ) {
221
+ if (!$ bProcessSubarrays ) {
220
222
continue ;
221
223
}
222
- $ result = self ::arrayStrPos ( $ mNeedle , $ mItem , $ bCaseInsensitive , $ bProcessSubarrays );
224
+ $ result = self ::arrayStrPos ($ mNeedle , $ mItem , $ bCaseInsensitive , $ bProcessSubarrays );
223
225
224
- if ( false !== $ result ) {
226
+ if (false !== $ result ) {
225
227
break ;
226
228
}
227
229
} else {
228
- if ( false !== ( $ bCaseInsensitive ? \mb_stripos ( $ mItem , $ mNeedle ) : \mb_strpos ( $ mItem , $ mNeedle ) ) ) {
230
+ if (false !== ($ bCaseInsensitive ? \mb_stripos ($ mItem , $ mNeedle ) : \mb_strpos ($ mItem , $ mNeedle)) ) {
229
231
$ result = $ key ;
232
+
230
233
break ;
231
234
}
232
235
}
@@ -243,73 +246,75 @@ public static function arrayStrPos(
243
246
* Массив объединённый в строку (к примеру, для сохранения в CSV).
244
247
* Вложенные подмассивы будут также объединены в строку с разделителем subseparator.
245
248
*
246
- * @param array $aHaystack
249
+ * @param array $aHaystack
247
250
* @param string $default
248
251
* @param string $separator
249
252
* @param string $subseparator
250
- * @param bool $isRecursion (service parameter)
253
+ * @param bool $isRecursion (service parameter)
251
254
*
252
255
* @return string converted array to string
253
- */
256
+ */
254
257
public static function arrayToCsvString (
255
258
array $ aHaystack ,
256
259
?string $ default = '' ,
257
260
?string $ separator = '; ' ,
258
261
?string $ subseparator = '| ' ,
259
262
?bool $ isRecursion = false
260
263
): string {
261
- if ( empty ( $ aHaystack ) ) {
264
+ if (empty ($ aHaystack) ) {
262
265
return $ default ;
263
266
}
264
267
265
268
$ separator = $ isRecursion ? $ separator : '" ' . $ separator . '" ' ;
266
269
267
270
$ res = [];
268
- foreach ( $ aHaystack as $ row ) {
269
- $ res [] = \is_array ( $ row ) ? \trim ( self ::arrayToCsvString ( $ row , $ default , $ subseparator , '|| ' , true ), "\"\n\r" ) : \trim ( $ row );
271
+ foreach ($ aHaystack as $ row ) {
272
+ $ res [] = \is_array ($ row ) ? \trim (self ::arrayToCsvString ($ row , $ default , $ subseparator , '|| ' , true ), "\"\n\r" ) : \trim ($ row );
270
273
}
271
274
272
- return '" ' . \implode ( $ separator , $ res ) . '" ' . PHP_EOL ;
275
+ return '" ' . \implode ($ separator , $ res ) . '" ' . PHP_EOL ;
273
276
}
274
277
275
278
/**
276
279
* Flatten Multidimensional Array.
277
280
*
278
281
* Превратить Многомерный Массив в одномерный.
279
282
*
280
- * @param array $aHaystack
281
- * @param string| bool $prefix
283
+ * @param array $aHaystack
284
+ * @param bool|string $prefix
282
285
*
283
286
* @return array
284
287
*
285
288
* @author https://github.com/php-curl-class/php-curl-class
289
+ *
286
290
* @edit Leonid Sheikman (leonid74)
287
291
*/
288
292
public static function arrayFlattenMulti (
289
293
?array $ aHaystack = [],
290
- bool $ prefix = false
294
+ $ prefix = false
291
295
): array {
292
296
$ aResult = [];
293
297
294
- if ( $ prefix && $ aHaystack === null ) {
298
+ if ($ prefix && $ aHaystack === null ) {
295
299
$ aResult [$ prefix ] = null ;
296
300
297
301
return $ aResult ;
298
302
}
299
303
300
- if ( $ prefix && empty ( $ aHaystack ) ) {
304
+ if ($ prefix && empty ($ aHaystack) ) {
301
305
$ aResult [$ prefix ] = '' ;
302
306
303
307
return $ aResult ;
304
308
}
305
309
306
- if ( !empty ( $ aHaystack ) ) {
307
- foreach ( $ aHaystack as $ key => $ val ) {
308
- if ( \is_scalar ( $ val ) ) {
309
- $ aResult [ $ prefix ? $ prefix . '[ ' . $ key . '] ' : $ key ] = $ val ;
310
+ if (!empty ($ aHaystack )) {
311
+ foreach ($ aHaystack as $ key => $ val ) {
312
+ if (\is_scalar ($ val )) {
313
+ $ aResult [$ prefix ? $ prefix . '[ ' . $ key . '] ' : $ key ] = $ val ;
314
+
310
315
continue ;
311
316
}
312
- $ aResult = \array_merge ( $ aResult , self ::arrayFlattenMulti ( $ val , $ prefix ? $ prefix . '[ ' . $ key . '] ' : $ key ) );
317
+ $ aResult = \array_merge ($ aResult , self ::arrayFlattenMulti ($ val , $ prefix ? $ prefix . '[ ' . $ key . '] ' : $ key) );
313
318
}
314
319
}
315
320
@@ -333,13 +338,13 @@ public static function arrayMapRecursive(
333
338
string $ func ,
334
339
array $ aHaystack = []
335
340
): array {
336
- if ( empty ( $ aHaystack ) ) {
341
+ if (empty ($ aHaystack) ) {
337
342
return [];
338
343
}
339
344
340
345
$ aResult = [];
341
- foreach ( $ aHaystack as $ key => $ value ) {
342
- $ aResult [$ key ] = ( \is_array ( $ value ) ? self ::arrayMapRecursive ( $ func , $ value ) : $ func ( $ value ) );
346
+ foreach ($ aHaystack as $ key => $ value ) {
347
+ $ aResult [$ key ] = (\is_array ($ value ) ? self ::arrayMapRecursive ($ func , $ value ) : $ func ($ value) );
343
348
}
344
349
345
350
return $ aResult ;
@@ -352,7 +357,7 @@ public static function arrayMapRecursive(
352
357
*
353
358
* @param array|string $search
354
359
* @param array|string $replace
355
- * @param array $aHaystack
360
+ * @param array $aHaystack
356
361
*
357
362
* @return array
358
363
*
@@ -363,9 +368,10 @@ public static function arrayStrReplaceMulti(
363
368
$ replace ,
364
369
array $ aHaystack = []
365
370
): array {
366
- if ( empty ( $ aHaystack ) ) {
371
+ if (empty ($ aHaystack) ) {
367
372
return [];
368
373
}
369
- return \json_decode ( \str_replace ( $ search , $ replace , \json_encode ( $ aHaystack , JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) ), true );
374
+
375
+ return \json_decode (\str_replace ($ search , $ replace , \json_encode ($ aHaystack , JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES )), true );
370
376
}
371
377
}
0 commit comments