From c143b3e19199b33f7807b1a0002024494d877b8a Mon Sep 17 00:00:00 2001 From: Runnerlee Date: Thu, 11 May 2017 16:48:55 +0800 Subject: [PATCH] fix array merge error --- src/ArrayObject.php | 7 ++++++- tests/ArrayObjectTest.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/ArrayObject.php b/src/ArrayObject.php index 0b26ee1..7359fab 100644 --- a/src/ArrayObject.php +++ b/src/ArrayObject.php @@ -33,7 +33,12 @@ public function merge($array) $merge = function ($array1, $array2) use (&$merge) { foreach ($array2 as $key => $value) { if (array_key_exists($key, $array1) && is_array($value)) { - $array1[$key] = $merge($array1[$key], $array2[$key]); + if (is_array($array1[$key])) { + $array1[$key] = $merge($array1[$key], $value); + } else { + array_unshift($value, $array1[$key]); + $array1[$key] = $value; + } } else { $array1[$key] = $value; } diff --git a/tests/ArrayObjectTest.php b/tests/ArrayObjectTest.php index da4135c..440c6f9 100644 --- a/tests/ArrayObjectTest.php +++ b/tests/ArrayObjectTest.php @@ -89,6 +89,35 @@ public function testMerge () ] ] ], $this->array->getArrayCopy()); + + + $this->array->merge([ + 'foo' => null, + ]); + + $this->array->merge([ + 'foo' => [ + 'bar' => 'var', + ], + ]); + + $this->assertSame( + [ + 'foo' => [ + 0 => NULL, + 'bar' => 'var', + ], + 'foobar' => 'zyc', + 'database' => [ + 'host' => '127.0.0.1', + 'options' => [ + 'foo' => 'bar', + 'foobar' => 'zyc', + ], + ], + ], + $this->array->getArrayCopy() + ); } public function testArrayKey()