Merge 2 arrays.

A more suitable replacement for array_merge_recursive.
Treats all arrays as associative (Does not append numeric keys).

@param $array1
@param $array2
@return array
514589
private static function configArrayMergeRecursive($array1, $array2) {
        if (is_array($array1) && is_array($array2)) {
            foreach ($array2 as $key => $value) {
                if (isset($array1[$key])) {
                    $array1[$key] = self::configArrayMergeRecursive($array1[$key], $array2[$key]);
                } else {
                    $array1[$key] = $value;
                }
            }
        } else {
            $array1 = $array2;
        }
        return $array1;
    }
20529
public static function arrayMergeNoDuplicates(array &$array1, array &$array2) {
    $merged = array_merge($array1, $array2);
    $merged_without_dups = array_unique($merged);
    $merged_rekeyed = array_values($merged_without_dups);

    return $merged_rekeyed;
  }
1100135
public static function array_diff_multi($array1, $array2)
    {

        $res = array();
        foreach ($array1 as $entry) {
            if (!in_array($entry, $array2)) {
                $res[] = $entry;
            }
        }

        return $res;
    }
1011006
protected static function smart_array_merge ($array1, $array2) {
		if (isset($array1['class'], $array2['class'])) {
			$array1['class'] .= " $array2[class]";
			unset($array2['class']);
		}
		if (isset($array1['style'], $array2['style'])) {
			$array1['style'] = trim($array1['style'], ';').";$array2[style]";
			unset($array2['style']);
		}
		return array_merge($array1, $array2);
	}
500060
public static function merge(array $array1, array $array2, $deep = false)
    {
        // Normal array merge
        if (!$deep) {
            return array_merge($array1, $array2);
        }

        // Perform a deep merge
        $merged = $array1;

        foreach ($array2 as $key => $value) {
            if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
                $merged[$key] = static::merge($merged[$key], $value, $deep);
            } else if (is_numeric($key)) {
                if (!in_array($value, $merged)) {
                    $merged[] = $value;
                }
            } else {
                $merged[$key] = $value;
            }
        }

        return $merged;
    }
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;
	}
620538
private function merge(array $merged, array $array)
    {
        foreach ($array as $key => $value) {
            if (is_array($value) && isset ($merged[$key]) && is_array($merged[$key])) {
                $merged[$key] = $this->merge($merged[$key], $value);
            } else {
                $merged[$key] = $value;
            }
        }

        return $merged;
    }
621979
public static function expand(array $array)
    {
        $result = array();

        foreach ($array as $key => $value) {
            $references = self::explodeKeys((string)$key); // cast for performance
            $value = self::expandValue($value);

            if (count($references) === 1) {
                $key = $references[0];

                // If the result key is already set, we have to merge
                // the two values.
                if (array_key_exists($key, $result)) {
                    $result[$key] = self::mergeTwoValues($result[$key], $value, array($key));
                }
                else {
                    $result[$key] = $value;
                }
            }
            else {
                self::dereferenceDots($result, $references, $value);
            }
        }

        return $result;
    }
1118068
protected function merge_arrays() {
        $arrays = func_get_args();
        $narrays = count($arrays);

        // check arguments
        // comment out if more performance is necessary (in this case the foreach loop will trigger a warning if the argument is not an array)
        for ($i = 0; $i < $narrays; $i ++) {
            if (!is_array($arrays[$i])) {
                // also array_merge_recursive returns nothing in this case
                trigger_error('Argument #' . ($i+1) . ' is not an array - trying to merge array with scalar! Returning false!', E_USER_WARNING);
                return false;
            }
        }

        // the first array is in the output set in every case
        $ret = $arrays[0];

        // merege $ret with the remaining arrays
        for ($i = 1; $i < $narrays; $i ++) {
            foreach ($arrays[$i] as $key => $value) {
                if (is_array($value) && isset($ret[$key])) {
                    // if $ret[$key] is not an array you try to merge an scalar value with an array - the result is not defined (incompatible arrays)
                    // in this case the call will trigger an E_USER_WARNING and the $ret[$key] will be false.
                    $ret[$key] = $this->merge_arrays($ret[$key], $value);
                } else {
                    $ret[$key] = $value;
                }
            }
        }

        return $ret;
    }
267177
private function mergeValues(array $set1, array $set2)
    {
        // `$set2` may be empty if an array value is chunked at the end of the
        // list. Handling it normally results in an additional `null` value
        // being pushed onto the list of values. Since this method is only
        // called in cases where two chunks must be merged, we can safely
        // short-circuit the operation of the second chunk is empty.
        if (empty($set2)) {
            return $set1;
        }

        $lastItemSet1 = array_pop($set1);
        $firstItemSet2 = array_shift($set2);
        $item = $firstItemSet2;

        if (is_string($lastItemSet1) && is_string($firstItemSet2)) {
            $item = $lastItemSet1 . $firstItemSet2;
        } elseif (is_array($lastItemSet1)) {
            $item = $this->mergeValues($lastItemSet1, $firstItemSet2);
        } else {
            array_push($set1, $lastItemSet1);
        }

        array_push($set1, $item);
        return array_merge($set1, $set2);
    }
903851
public function merge(array $a, array $b)
    {
        foreach ($b as $key => $value) {
            if (array_key_exists($key, $a)) {
                if (is_int($key)) {
                    $a[] = $value;
                } elseif (is_array($value) && is_array($a[$key])) {
                    $a[$key] = $this->merge($a[$key], $value);
                } else {
                    $a[$key] = $value;
                }
            } else {
                $a[$key] = $value;
            }
        }

        return $a;
    }
612990
protected function merge(\stdClass $object1, \stdClass $object2)
    {
        return array_to_object(
            array_replace_recursive(object_to_array($object1), object_to_array($object2))
        );
    }
1101103
public static function merge(array $source1, array $source2): array
    {
        $return = $source2;

        foreach ($source1 as $key => $value) {
            if ($value && is_array($value) && isset($source2[$key]) && is_array($source2[$key])) {
                $value = array_replace_recursive($source2[$key], $value);
            }
            $return[$key] = $value;
        }

        return $return;
    }
232236
protected function merge($a, $b)
    {
        if (!is_array($a)) {
            if (!is_array($b)) return [];
            return $b;
        }

        if (!is_array($b)) return $a;

        foreach($b as $k => $v) {
            if (!isset($a[$k])) {
                $a[$k] = $v;
                continue;
            }
            if (!is_array($v)) {
                if (!is_array($a[$k])) {
                    $a[$k] = $v;
                    continue;
                }
                $a[$k] = array_merge($a[$k], [$v]);
                foreach(array_keys($a[$k]) as $kk) {
                    if (is_numeric($kk)) {
                        $a[$k] = array_unique($a[$k]);
                        break;
                    }
                }
                continue;
            }
            $a[$k] = array_merge($a[$k], $b[$k]);
            foreach(array_keys($a[$k]) as $kk) {
                if (is_numeric($kk)) {
                    $a[$k] = array_unique($a[$k]);
                    break;
                }
            }
        }

        return $a;
    }
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;
    }
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;
    }
853628
function merge(arrays, comp) {
		var mid,
			a1, i1,
			a2, i2,
			result;

		if (arrays.length === 1) {
			return arrays[0];
		} else if (arrays.length === 2) {
			// merge two arrays
			a1 = arrays[0];
			a2 = arrays[1];
			i1 = i2 = 0;
			result = [];
			while(i1 < a1.length && i2 < a2.length) {
				if (comp(a2[i2], a1[i1]) > 0) {
					result.push(a1[i1]);
					i1++;
				} else {
					result.push(a2[i2]);
					i2++;
				}
			}
			while(i1 < a1.length) {
				result.push(a1[i1]);
				i1++;
			}
			while(i2 < a2.length) {
				result.push(a2[i2]);
				i2++;
			}
			return result;
		} else {
			mid = Math.floor(arrays.length / 2);
			return merge([
				merge(arrays.slice(0, mid), comp),
				merge(arrays.slice(mid), comp)
			], comp);
		}

	}