Nette\Arrays::mergeTree

Recursively appends elements of remaining keys from the second array to the first.

@param mixed[] $arr1
@param mixed[] $arr2
@return mixed[]
197398
public static function mergeTree(array $arr1, array $arr2): array
	{
		$res = $arr1 + $arr2;
		foreach (array_intersect_key($arr1, $arr2) as $k => $v) {
			if (is_array($v) && is_array($arr2[$k])) {
				$res[$k] = self::mergeTree($v, $arr2[$k]);
			}
		}
		return $res;
	}
36740
public static function array_merge_recursive_unique($array1, $array2)
    {
        // LOOP THROUGH $array2
        foreach ($array2 as $k => $v) {

            // CHECK IF VALUE EXISTS IN $array1
            if (!empty($array1[$k])) {
                // IF VALUE EXISTS CHECK IF IT'S AN ARRAY OR A STRING
                if (!is_array($array2[$k])) {
                    // OVERWRITE IF IT'S A STRING
                    $array1[$k]=$array2[$k];
                } else {
                    // RECURSE IF IT'S AN ARRAY
                    $array1[$k] = self::array_merge_recursive_unique($array1[$k], $array2[$k]);
                }
            } else {
                // IF VALUE DOESN'T EXIST IN $array1 USE $array2 VALUE
                $array1[$k]=$v;
            }
        }
        unset($k, $v);

        return $array1;
    }
279211
private function _mergeDependencyVersions(array $array1, array $array2) {
        foreach ($array2 as $package => $version) {
            if (!isset($array1[$package])) {
                $array1[$package] = $version;
            } else {
                if ($array1[$package] != $version) {
                    $array1[$package] .= " ".$version;
                }
            }
        }
        return $array1;
    }
1353034
public static function recursiveMerge(...$arrays)
    {
        $merged = array ();
        foreach($arrays as $array)
        {
            if (!is_array($array))
            {
                continue;
            }
            foreach ( $array as $key => $value )
            {
                if (is_string ( $key ))
                {
                    if (is_array ( $value ) && isset($merged[$key]) && is_array ( $merged [$key] ))
                    {
                        $merged [$key] = static::recursiveMerge ( $merged [$key], $value );
                    }
                    else
                    {
                        $merged [$key] = $value;
                    }
                }
                else
                {
                    $merged [] = $value;
                }
            }
        }
        return $merged;
    }
172109
public static function array_add(array $arr1, array $arr2)
    {
        $newArr = $arr1 ? $arr1 : [];
        foreach ($arr2 as $key => $value) {
            $newArr[] = $value;
        }
        return $newArr;
    }
496758
public function mergeAppendKeepIndex(array $array = [], bool $recursive = false): self
    {
        if ($recursive === true) {
            $result = \array_replace_recursive($this->getArray(), $array);
        } else {
            $result = \array_replace($this->getArray(), $array);
        }

        return static::create(
            $result,
            $this->iteratorClass,
            false
        );
    }
1212548
public function merge(array $array, $recursive = false)
    {
        if ($recursive) {
            $this->data = static::mergeRecursive($this->data, $array);
        } else {
            $this->data = array_merge($array);
        }

        return $this;
    }
143730
protected function arrayMergeRecursiveOverwrite(array $array1, array $array2, array $array = []) {
        $arrays = func_get_args();
        $merged = [];

        while ($arrays) {
            $array = array_shift($arrays);

            if (!is_array($array)) {
                continue;
            }

            foreach ($array as $key => $value) {
                if (is_string($key)) {
                    // Merged keys are both arrays, last one has values:
                    if (is_array($value) &&
                        array_key_exists($key, $merged) &&
                        is_array($merged[$key]) &&
                        count($value) > 0) {
                        if (array_keys($value) !== range(0, count($value) - 1)) {
                            // Associative array or zero-indexed, but not sequential array:
                            $merged[$key] = call_user_func([$this, __FUNCTION__], $merged[$key], $value);
                        } else {
                            // Zero-indexed and sequential array:
                            $merged[$key] = $value;
                        }
                    } else {
                        // Value either not an array or an empty array or original is not an array:
                        $merged[$key] = $value;
                    }
                } elseif (!in_array($value, $merged)) {
                    $merged[] = $value;
                }
            }
        }

        return $merged;
    }
534131
private function associativeArrayIntersect(array $arr1, array $arr2)
    {
        $commonKeys = array_intersect(array_keys($arr1), array_keys($arr2));

        $ret = [];
        foreach ($commonKeys as $key) {
            $return = $this->arrayIntersectRecursive($arr1[$key], $arr2[$key]);
            if ($return !== false) {
                $ret[$key] = $return;
                continue;
            }
            if ($this->isEqualValue($arr1[$key], $arr2[$key])) {
                $ret[$key] = $arr1[$key];
            }
        }

        if (empty($commonKeys)) {
            foreach ($arr2 as $arr) {
                $return = $this->arrayIntersectRecursive($arr1, $arr);
                if ($return && $return == $arr1) {
                    return $return;
                }
            }
        }

        if (count($ret) < min(count($arr1), count($arr2))) {
            return null;
        }

        return $ret;
    }
525360
public static function array_merge_recursive_distinct (array $array1, array $array2)
    {
        $result = $array1;

        foreach ($array2 as $key => $val) {
            if (is_array($array2[$key])) {
                $result[$key] = is_array($result[$key]) ? self::array_merge_recursive_distinct($result[$key], $array2[$key]) : $array2[$key];
            } else {
                $result[$key] = $val;
            }
        }

        return $result;
    }
683405
function array_merge_recursive_unique($array1, $array2)
	{
		$arrays = func_get_args();
		$narrays = count($arrays);
		$ret = $arrays[0];
		for ($i = 1; $i < $narrays; $i ++) {
			foreach ($arrays[$i] as $key => $value) {
				if (((string) $key) === ((string)((int) $key))) { // integer or string as integer key - append
					$ret[] = $value;
				} else { // string key - merge
					if (is_array($value) && isset($ret[$key])) {
						$ret[$key] = $this->array_merge_recursive_unique($ret[$key], $value);
					} else {
						$ret[$key] = $value;
					}
				}
			}
		}
		return $ret;
	}
398358
public static function rearrangePostedFiles($arr)
    {
        $new = array();
        foreach ($arr as $key => $all) {
            if (is_array($all)) {
                foreach ($all as $i => $val) {
                    $new[$i][$key] = $val;
                }
            } else {
                $new[0][$key] = $all;
            }
        }

        return $new;
    }
1231089
protected static function mergeKeyed($file1_contents, $file2_contents, $exclude_keys = []) {
    // Merge keyed arrays objects.
    $merge_keys = [
      'config',
      'extra',
    ];
    $output = $file1_contents;
    foreach ($merge_keys as $key) {
      // Set empty keys to empty placeholder arrays.
      if (!array_key_exists($key, $file1_contents)) {
        $file1_contents[$key] = [];
      }
      if (!array_key_exists($key, $file2_contents)) {
        $file2_contents[$key] = [];
      }

      // Merge!
      $output[$key] = ArrayManipulator::arrayMergeRecursiveDistinct($file1_contents[$key], $file2_contents[$key]);
    }

    return $output;
  }
899096
protected function _arrayMergeRecursive($firstArray, $secondArray)
    {
        if (is_array($firstArray) && is_array($secondArray)) {
            foreach ($secondArray as $key => $value) {
                if (isset($firstArray[$key])) {
                    $firstArray[$key] = $this->_arrayMergeRecursive($firstArray[$key], $value);
                } else {
                    if($key === 0) {
                        $firstArray= array(0=>$this->_arrayMergeRecursive($firstArray, $value));
                    } else {
                        $firstArray[$key] = $value;
                    }
                }
            }
        } else {
            $firstArray = $secondArray;
        }

        return $firstArray;
    }
691846
public static function mergeOptions($options1, $options2, $recursive = true)
    {
        if (!$recursive) {
            return array_merge($options1, $options2);
        }

        return Arr::mergeRecursive($options1, $options2);
    }
760780
public function mergeData(array $data1, array $data2, $name = null, $separator = '.')
    {
        $nested = $this->getNested($name, $separator);

        if (!\is_array($nested)) {
            $nested = [];
        }

        return $this->mergeArrays($data1, $data2, $nested);
    }
620017
public static function merge_data_recursive($a, $b)
    {
        $args = func_get_args();
        $t = $args[0];
        $array = is_array($t);

        foreach ($args as $k => $a) {
            $args[$k] = self::ar($a, null, true);
        }

        $args = call_user_func_array('array_merge_recursive', $args);
        
        if (!$array) {
            $args = self::sc($args, null, true);
        }

        return $args;
    }