Example #1
0
 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']]]]);
 }
Example #2
0
/**
 * 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;
}
Example #3
0
 public function merge(array $data)
 {
     $this->data = delim_merge($this->data, $data, $this->getDelimiter(), true);
 }