Skip to content

Commit eaf7d36

Browse files
committed
修正Arr::mergeDeep
1 parent a9b7702 commit eaf7d36

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

src/helper/Arr.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,7 @@ public static function mergeDeep(array ...$arrays): array
637637
$result = [];
638638
foreach ($arrays as $array) {
639639
foreach ($array as $key => $value) {
640-
if (is_integer($key)) {
641-
$result[] = $value;
642-
} elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
640+
if (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
643641
$result[$key] = self::mergeDeep(
644642
$result[$key],
645643
$value

tests/ArrTest.php

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function testCrossJoin()
5555

5656
public function testDivide()
5757
{
58-
list($keys, $values) = Arr::divide(['name' => 'ThinkPHP']);
58+
[$keys, $values] = Arr::divide(['name' => 'ThinkPHP']);
5959
$this->assertSame(['name'], $keys);
6060
$this->assertSame(['ThinkPHP'], $values);
6161
}
@@ -109,7 +109,7 @@ public function testFirst()
109109
public function testLast()
110110
{
111111
$array = [100, 200, 300];
112-
$last = Arr::last($array, function ($value) {
112+
$last = Arr::last($array, function ($value) {
113113
return $value < 250;
114114
});
115115
$this->assertSame(200, $last);
@@ -234,17 +234,17 @@ public function testPrepend()
234234
public function testPull()
235235
{
236236
$array = ['name' => 'ThinkPHP', 'price' => 100];
237-
$name = Arr::pull($array, 'name');
237+
$name = Arr::pull($array, 'name');
238238
$this->assertSame('ThinkPHP', $name);
239239
$this->assertSame(['price' => 100], $array);
240240
// Only works on first level keys
241241
$array = ['[email protected]' => 'Joe', 'jack@localhost' => 'Jane'];
242-
$name = Arr::pull($array, '[email protected]');
242+
$name = Arr::pull($array, '[email protected]');
243243
$this->assertSame('Joe', $name);
244244
$this->assertSame(['jack@localhost' => 'Jane'], $array);
245245
// Does not work for nested keys
246246
$array = ['emails' => ['[email protected]' => 'Joe', 'jack@localhost' => 'Jane']];
247-
$name = Arr::pull($array, '[email protected]');
247+
$name = Arr::pull($array, '[email protected]');
248248
$this->assertNull($name);
249249
$this->assertSame(['emails' => ['[email protected]' => 'Joe', 'jack@localhost' => 'Jane']], $array);
250250
}
@@ -331,12 +331,42 @@ public function testForget()
331331

332332
public function testWrap()
333333
{
334-
$string = 'a';
335-
$array = ['a'];
336-
$object = new stdClass();
334+
$string = 'a';
335+
$array = ['a'];
336+
$object = new stdClass();
337337
$object->value = 'a';
338338
$this->assertSame(['a'], Arr::wrap($string));
339339
$this->assertSame($array, Arr::wrap($array));
340340
$this->assertSame([$object], Arr::wrap($object));
341341
}
342+
343+
public function testMergeDeep()
344+
{
345+
$this->assertSame(
346+
[
347+
'a' => [
348+
'c' => [2],
349+
'e' => 5,
350+
'f' => 4,
351+
],
352+
'x' => 3,
353+
],
354+
Arr::mergeDeep(
355+
[
356+
'a' => [
357+
'c' => [1],
358+
'e' => 5,
359+
],
360+
'x' => 4,
361+
],
362+
[
363+
'a' => [
364+
'c' => [2],
365+
'f' => 4,
366+
],
367+
'x' => 3,
368+
]
369+
)
370+
);
371+
}
342372
}

0 commit comments

Comments
 (0)