public function testArrayDelimMerge() { $array = ['test' => ['test1' => 1, 'test2' => 2]]; $array2 = ['test' => ['test1' => 0, 'test2' => 10, 'test3' => 3, 'test4' => 4]]; delim_merge($array, $array2); $this->assertEquals($array, ['test' => ['test1' => 0, 'test2' => 10, 'test3' => 3, 'test4' => 4]]); $array3 = ['test.test4' => 14, 'testing.test.test' => ['mister' => 'shiznet']]; delim_merge($array, $array3); $this->assertEquals($array, ['test' => ['test1' => 0, 'test2' => 10, 'test3' => 3, 'test4' => 14], 'testing' => ['test' => ['test' => ['mister' => 'shiznet']]]]); }
/** * Merges two arrays by delimiter-separated paths * * @param array|ArrayIterator &$target [description] * @param array $source [description] * * @return [type] [description] */ function delim_merge(&$target, array $source, $delim = '.') { delim_expand($target, $delim); delim_expand($source, $delim); foreach ($source as $key => $value) { if (is_numeric($key)) { array_push($target, $value); } else { if (is_array($value)) { if (isset($target[$key])) { if (is_array($target[$key])) { $target[$key] = delim_merge($target[$key], $value, $delim); } else { $target[$key] = $value; } } else { $target[$key] = $value; } } else { $target[$key] = $value; } } } return $target; }
public function merge(array $data) { $this->data = delim_merge($this->data, $data, $this->getDelimiter(), true); }