Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit 39a4b55

Browse files
committed
update some info
1 parent 2fb6858 commit 39a4b55

File tree

1 file changed

+60
-36
lines changed

1 file changed

+60
-36
lines changed

src/ArrayHelper.php

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public static function accessible($value): bool
3535
*/
3636
public static function isAssoc(array $array): bool
3737
{
38-
$keys = array_keys($array);
38+
$keys = \array_keys($array);
3939

40-
return array_keys($keys) !== $keys;
40+
return \array_keys($keys) !== $keys;
4141
}
4242

4343
/**
@@ -64,9 +64,9 @@ public static function toObject($array, $class = \stdClass::class)
6464
$object = new $class;
6565

6666
foreach ($array as $name => $value) {
67-
$name = trim($name);
67+
$name = \trim($name);
6868

69-
if (!$name || is_numeric($name)) {
69+
if (!$name || \is_numeric($name)) {
7070
continue;
7171
}
7272

@@ -104,7 +104,7 @@ public static function gets(array &$data, array $needKeys = [], $unsetKey = fals
104104
if (\is_int($default)) {
105105
$value = (int)$value;
106106
} elseif (\is_string($default)) {
107-
$value = trim($value);
107+
$value = \trim($value);
108108
} elseif (\is_array($default)) {
109109
$value = (array)$value;
110110
}
@@ -145,7 +145,7 @@ public static function merge($src, array $new): array
145145
} else {
146146
$src[$key] = $value;
147147
}
148-
} elseif (array_key_exists($key, $src) && \is_array($value)) {
148+
} elseif (\array_key_exists($key, $src) && \is_array($value)) {
149149
$src[$key] = self::merge($src[$key], $new[$key]);
150150
} else {
151151
$src[$key] = $value;
@@ -196,12 +196,12 @@ public static function merge2(...$args): array
196196
*/
197197
public static function valueTrim(array $data)
198198
{
199-
if (is_scalar($data)) {
200-
return trim($data);
199+
if (\is_scalar($data)) {
200+
return \trim($data);
201201
}
202202

203-
array_walk_recursive($data, function (&$value) {
204-
$value = trim($value);
203+
\array_walk_recursive($data, function (&$value) {
204+
$value = \trim($value);
205205
});
206206

207207
return $data;
@@ -215,7 +215,7 @@ public static function valueTrim(array $data)
215215
*/
216216
public static function keyExists($key, array $arr): bool
217217
{
218-
return array_key_exists(strtolower($key), array_change_key_case($arr));
218+
return \array_key_exists(\strtolower($key), \array_change_key_case($arr));
219219
}
220220

221221
/**
@@ -251,7 +251,7 @@ public static function changeValueCase($arr, $toUpper = 1): array
251251
if (\is_array($v)) {
252252
$newArr[$k] = self::changeValueCase($v, $toUpper);
253253
} else {
254-
$v = trim($v);
254+
$v = \trim($v);
255255
$newArr[$k] = $function($v);
256256
}
257257
}
@@ -271,11 +271,11 @@ public static function valueExistsAll($check, array $sampleArr): bool
271271
{
272272
// 以逗号分隔的会被拆开,组成数组
273273
if (\is_string($check)) {
274-
$check = trim($check, ', ');
275-
$check = strpos($check, ',') !== false ? explode(',', $check) : [$check];
274+
$check = \trim($check, ', ');
275+
$check = \strpos($check, ',') !== false ? explode(',', $check) : [$check];
276276
}
277277

278-
return !array_diff((array)$check, $sampleArr);
278+
return !\array_diff((array)$check, $sampleArr);
279279
}
280280

281281
/**
@@ -289,11 +289,11 @@ public static function valueExistsOne($check, array $sampleArr): bool
289289
{
290290
// 以逗号分隔的会被拆开,组成数组
291291
if (\is_string($check)) {
292-
$check = trim($check, ', ');
293-
$check = strpos($check, ',') !== false ? explode(',', $check) : [$check];
292+
$check = \trim($check, ', ');
293+
$check = \strpos($check, ',') !== false ? explode(',', $check) : [$check];
294294
}
295295

296-
return (bool)array_intersect((array)$check, $sampleArr);
296+
return (bool)\array_intersect((array)$check, $sampleArr);
297297
}
298298

299299
/**
@@ -377,8 +377,8 @@ public static function getKeyMaxWidth(array $data, $expectInt = true): int
377377

378378
foreach ($data as $key => $value) {
379379
// key is not a integer
380-
if (!$expectInt || !is_numeric($key)) {
381-
$width = mb_strlen($key, 'UTF-8');
380+
if (!$expectInt || !\is_numeric($key)) {
381+
$width = \mb_strlen($key, 'UTF-8');
382382
$keyMaxWidth = $width > $keyMaxWidth ? $width : $keyMaxWidth;
383383
}
384384
}
@@ -426,6 +426,29 @@ public static function getByPath($data, string $path, $default = null, string $s
426426
return $dataTmp;
427427
}
428428

429+
/**
430+
* findValueByNodes
431+
* @param array $data
432+
* @param array $nodes
433+
* @param mixed $default
434+
* @return mixed
435+
*/
436+
public static function getValueByNodes(array $data, array $nodes, $default = null)
437+
{
438+
$temp = $data;
439+
440+
foreach ($nodes as $name) {
441+
if (isset($temp[$name])) {
442+
$temp = $temp[$name];
443+
} else {
444+
$temp = $default;
445+
break;
446+
}
447+
}
448+
449+
return $temp;
450+
}
451+
429452
/**
430453
* setByPath
431454
* @param array|\ArrayAccess &$data
@@ -435,12 +458,12 @@ public static function getByPath($data, string $path, $default = null, string $s
435458
*/
436459
public static function setByPath(&$data, string $path, $value, string $separator = '.')
437460
{
438-
if (false === strpos($path, $separator)) {
461+
if (false === \strpos($path, $separator)) {
439462
$data[$path] = $value;
440463
return;
441464
}
442465

443-
if (!$nodes = array_filter(explode($separator, $path))) {
466+
if (!$nodes = \array_filter(\explode($separator, $path))) {
444467
return;
445468
}
446469

@@ -473,7 +496,7 @@ public static function setByPath(&$data, string $path, $value, string $separator
473496
* @param array $array
474497
* @return array
475498
*/
476-
public static function collapse($array): array
499+
public static function collapse(array $array): array
477500
{
478501
$results = [];
479502

@@ -484,10 +507,11 @@ public static function collapse($array): array
484507
continue;
485508
}
486509

487-
$results = \array_merge($results, $values);
510+
// $results = \array_merge($results, $values);
511+
$results[] = $values;
488512
}
489513

490-
return $results;
514+
return \array_merge(...$results);
491515
}
492516

493517
/**
@@ -513,7 +537,7 @@ public static function crossJoin(...$arrays): array
513537
*/
514538
public static function divide($array): array
515539
{
516-
return [array_keys($array), array_values($array)];
540+
return [\array_keys($array), \array_values($array)];
517541
}
518542

519543
/**
@@ -522,7 +546,7 @@ public static function divide($array): array
522546
* @param string $prepend
523547
* @return array
524548
*/
525-
public static function dot($array, $prepend = ''): array
549+
public static function dot(array $array, string $prepend = ''): array
526550
{
527551
$results = [];
528552

@@ -543,7 +567,7 @@ public static function dot($array, $prepend = ''): array
543567
* @param array|string $keys
544568
* @return array
545569
*/
546-
public static function except($array, $keys): array
570+
public static function except(array $array, $keys): array
547571
{
548572
static::forget($array, $keys);
549573

@@ -556,7 +580,7 @@ public static function except($array, $keys): array
556580
* @param string|int $key
557581
* @return bool
558582
*/
559-
public static function exists($array, $key): bool
583+
public static function exists(array $array, $key): bool
560584
{
561585
if ($array instanceof \ArrayAccess) {
562586
return $array->offsetExists($key);
@@ -572,7 +596,7 @@ public static function exists($array, $key): bool
572596
* @param mixed $value
573597
* @return array
574598
*/
575-
public static function add($array, $key, $value): array
599+
public static function add(array $array, $key, $value): array
576600
{
577601
if (static::has($array, $key)) {
578602
static::set($array, $key, $value);
@@ -621,13 +645,13 @@ public static function get($array, $key, $default = null)
621645
* @param mixed $value
622646
* @return array
623647
*/
624-
public static function set(&$array, $key, $value): array
648+
public static function set(array &$array, $key, $value): array
625649
{
626650
if (null === $key) {
627651
return ($array = $value);
628652
}
629653

630-
$keys = explode('.', $key);
654+
$keys = \explode('.', $key);
631655

632656
while (\count($keys) > 1) {
633657
$key = array_shift($keys);
@@ -641,7 +665,7 @@ public static function set(&$array, $key, $value): array
641665
$array = &$array[$key];
642666
}
643667

644-
$array[array_shift($keys)] = $value;
668+
$array[\array_shift($keys)] = $value;
645669

646670
return $array;
647671
}
@@ -658,14 +682,14 @@ public static function flatten($array, $depth = INF): array
658682
$item = $item instanceof CollectionInterface ? $item->all() : $item;
659683

660684
if (!\is_array($item)) {
661-
return array_merge($result, [$item]);
685+
return \array_merge($result, [$item]);
662686
}
663687

664688
if ($depth === 1) {
665-
return array_merge($result, array_values($item));
689+
return \array_merge($result, \array_values($item));
666690
}
667691

668-
return array_merge($result, static::flatten($item, $depth - 1));
692+
return \array_merge($result, static::flatten($item, $depth - 1));
669693
}, []);
670694
}
671695

0 commit comments

Comments
 (0)