Skip to content

Commit

Permalink
Merge pull request #2 from JanHuang/0.1
Browse files Browse the repository at this point in the history
0.1
  • Loading branch information
JanHuang authored May 11, 2017
2 parents a83a7de + 449a4ce commit 5dfa3c1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/ArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/ArrayObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 5dfa3c1

Please sign in to comment.