Skip to content

Commit 78c63f7

Browse files
committed
up: update some for array merge util
1 parent 691a5fd commit 78c63f7

File tree

5 files changed

+14
-18
lines changed

5 files changed

+14
-18
lines changed

src/Arr/ArrayHelper.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
use function mb_strlen;
4242
use function method_exists;
4343
use function strlen;
44-
use function strpos;
4544
use function strtolower;
4645
use function trim;
4746

src/Arr/Traits/ArrayMergeTrait.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace Toolkit\Stdlib\Arr\Traits;
1111

1212
use RuntimeException;
13-
use function array_key_exists;
1413
use function array_merge;
1514
use function array_shift;
1615
use function is_array;
@@ -71,16 +70,18 @@ public static function quickMerge(array $append, array $base): array
7170
}
7271

7372
/**
73+
* Recursion merge new array to src array.
7474
* 递归合并两个多维数组,后面的值将会递归覆盖原来的值
7575
*
76-
* @param array|null $src
77-
* @param array $new
76+
* @param array $src
77+
* @param array $new
78+
* @param int $depth max merge depth
7879
*
7980
* @return array
8081
*/
81-
public static function merge($src, array $new): array
82+
public static function merge(array $src, array $new, int $depth = 10): array
8283
{
83-
if (!$src || !is_array($src)) {
84+
if (!$src) {
8485
return $new;
8586
}
8687

@@ -95,8 +96,8 @@ public static function merge($src, array $new): array
9596
} else {
9697
$src[$key] = $value;
9798
}
98-
} elseif (array_key_exists($key, $src) && is_array($value)) {
99-
$src[$key] = self::merge($src[$key], $new[$key]);
99+
} elseif ($depth > 0 && isset($src[$key]) && is_array($value)) {
100+
$src[$key] = self::merge($src[$key], $value, --$depth);
100101
} else {
101102
$src[$key] = $value;
102103
}

src/Arr/Traits/ArrayValueGetSetTrait.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use function is_int;
2121
use function is_object;
2222
use function is_string;
23-
use function strpos;
2423
use function trim;
2524

2625
/**
@@ -203,7 +202,7 @@ public static function getByPath($data, string $path, $default = null, string $s
203202
}
204203

205204
/**
206-
* findValueByNodes
205+
* find Value By Nodes
207206
*
208207
* @param array $data
209208
* @param array $nodes
@@ -237,7 +236,7 @@ public static function getValueByNodes(array $data, array $nodes, $default = nul
237236
*/
238237
public static function setByPath(&$data, string $path, $value, string $separator = '.'): void
239238
{
240-
if (false === strpos($path, $separator)) {
239+
if (!str_contains($path, $separator)) {
241240
$data[$path] = $value;
242241
return;
243242
}
@@ -247,7 +246,6 @@ public static function setByPath(&$data, string $path, $value, string $separator
247246
}
248247

249248
$dataTmp = &$data;
250-
251249
foreach ($nodes as $node) {
252250
if (is_array($dataTmp)) {
253251
if (empty($dataTmp[$node])) {

src/Helper/PhpHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public static function call($cb, ...$args)
174174
{
175175
if (is_string($cb)) {
176176
// function
177-
if (strpos($cb, '::') === false) {
177+
if (!str_contains($cb, '::')) {
178178
return $cb(...$args);
179179
}
180180

src/Std/Collection.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,13 +354,11 @@ public function offsetSet($key, $value)
354354
/**
355355
* Remove item from collection
356356
*
357-
* @param string $key The data key
358-
*
359-
* @return mixed|null
357+
* @param string $offset The data key
360358
*/
361-
public function offsetUnset($key): void
359+
public function offsetUnset($offset): void
362360
{
363-
$this->remove($key);
361+
$this->remove($offset);
364362
}
365363

366364
/********************************************************************************

0 commit comments

Comments
 (0)