@@ -159,7 +159,7 @@ public static function keyExists($key, array $arr): bool
159
159
*/
160
160
public static function valueToLower (array $ arr ): array
161
161
{
162
- return self ::changeValueCase ($ arr , 0 );
162
+ return self ::changeValueCase ($ arr , false );
163
163
}
164
164
165
165
/**
@@ -175,12 +175,12 @@ public static function valueToUpper(array $arr): array
175
175
/**
176
176
* 将数组中的值全部转为大写或小写
177
177
*
178
- * @param array $arr
179
- * @param int $toUpper 1 值大写 0 值小写
178
+ * @param array|iterable $arr
179
+ * @param bool $toUpper
180
180
*
181
181
* @return array
182
182
*/
183
- public static function changeValueCase ($ arr , $ toUpper = 1 ): array
183
+ public static function changeValueCase ($ arr , bool $ toUpper = true ): array
184
184
{
185
185
$ function = $ toUpper ? 'strtoupper ' : 'strtolower ' ;
186
186
$ newArr = []; //格式化后的数组
@@ -248,7 +248,7 @@ public static function valueExistsOne($check, array $sampleArr): bool
248
248
*
249
249
* @return bool | string 不存在的会返回 检查到的 字段,判断时 请使用 ArrHelper::existsAll($need,$arr)===true 来验证是否全存在
250
250
*/
251
- public static function existsAll ($ need , $ arr , $ type = false )
251
+ public static function existsAll ($ need , $ arr , bool $ type = false )
252
252
{
253
253
if (is_array ($ need )) {
254
254
foreach ((array )$ need as $ v ) {
@@ -274,12 +274,12 @@ public static function existsAll($need, $arr, $type = false)
274
274
* 有一个存在就返回 true 都不存在 return false
275
275
*
276
276
* @param string|array $need
277
- * @param array $arr 只能检查一维数组
277
+ * @param array|iterable $arr 只能检查一维数组
278
278
* @param bool $type 是否同时验证类型
279
279
*
280
280
* @return bool
281
281
*/
282
- public static function existsOne ($ need , $ arr , $ type = false ): bool
282
+ public static function existsOne ($ need , $ arr , bool $ type = false ): bool
283
283
{
284
284
if (is_array ($ need )) {
285
285
foreach ((array )$ need as $ v ) {
@@ -309,28 +309,59 @@ public static function existsOne($need, $arr, $type = false): bool
309
309
/**
310
310
* get key Max Width
311
311
*
312
- * @param array $data
313
- * [
312
+ * ```php
313
+ * $data = [
314
314
* 'key1' => 'value1',
315
315
* 'key2-test' => 'value2',
316
- * ]
317
- * @param bool $expectInt
316
+ * ]
317
+ * ```
318
+ *
319
+ * @param array $data
320
+ * @param bool $excludeInt
318
321
*
319
322
* @return int
320
323
*/
321
- public static function getKeyMaxWidth (array $ data , $ expectInt = true ): int
324
+ public static function getKeyMaxWidth (array $ data , bool $ excludeInt = true ): int
322
325
{
323
- $ keyMaxWidth = 0 ;
324
-
326
+ $ maxWidth = 0 ;
325
327
foreach ($ data as $ key => $ value ) {
326
328
// key is not a integer
327
- if (!$ expectInt || !is_numeric ($ key )) {
328
- $ width = mb_strlen ($ key , 'UTF-8 ' );
329
- $ keyMaxWidth = $ width > $ keyMaxWidth ? $ width : $ keyMaxWidth ;
329
+ if (!$ excludeInt || !is_numeric ($ key )) {
330
+ $ width = mb_strlen ($ key , 'UTF-8 ' );
331
+ $ maxWidth = $ width > $ maxWidth ? $ width : $ maxWidth ;
332
+ }
333
+ }
334
+
335
+ return $ maxWidth ;
336
+ }
337
+
338
+ /**
339
+ * get max width
340
+ *
341
+ * ```php
342
+ * $keys = [
343
+ * 'key1',
344
+ * 'key2-test',
345
+ * ]
346
+ * ```
347
+ *
348
+ * @param array $keys
349
+ * @param bool $excludeInt
350
+ *
351
+ * @return int
352
+ */
353
+ public static function getMaxWidth (array $ keys , bool $ excludeInt = true ): int
354
+ {
355
+ $ maxWidth = 0 ;
356
+ foreach ($ keys as $ key ) {
357
+ // key is not a integer
358
+ if (!$ excludeInt || !is_numeric ($ key )) {
359
+ $ keyWidth = mb_strlen ($ key , 'UTF-8 ' );
360
+ $ maxWidth = $ keyWidth > $ maxWidth ? $ keyWidth : $ maxWidth ;
330
361
}
331
362
}
332
363
333
- return $ keyMaxWidth ;
364
+ return $ maxWidth ;
334
365
}
335
366
336
367
////////////////////////////////////////////////////////////
@@ -606,8 +637,8 @@ public static function remove(array &$arr, $key, $default = null)
606
637
/**
607
638
* Get a value from the array, and remove it.
608
639
*
609
- * @param array $array
610
- * @param string $key
640
+ * @param array|ArrayAccess $array
641
+ * @param string|int $key
611
642
* @param mixed $default
612
643
*
613
644
* @return mixed
0 commit comments