Пример #1
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;
}
Пример #2
0
 public function testArrayDelimExpand()
 {
     $array = ['testing.test.test' => 1, 'testing.test.test2' => 2, 'testing.test.test3' => 2, 'testing' => ['test2' => ['test.test' => '11111']]];
     delim_expand($array);
     $this->assertEquals(['testing' => ['test' => ['test' => 1, 'test2' => 2, 'test3' => 2], 'test2' => ['test' => ['test' => '11111']]]], $array);
 }