src/Util/ArrayUtils.php
public static function recursiveMerge($array1, $array2) {

        if ($array1 instanceof AssociativeArray) {
            $array1 = $array1->toArray();
        }

        if ($array2 instanceof AssociativeArray) {
            $array2 = $array2->toArray();
        }

        if (ArrayUtils::isAssociative($array1) != ArrayUtils::isAssociative($array2)) {

            if (ArrayUtils::isAssociative($array1)) {
                $array1 = array($array1);
            } else {
                $array2 = array($array2);
            }
        }

        $combinedKeys = array_unique(array_merge(array_keys($array1), array_keys($array2)));

        $mergedArray = array();
        foreach ($combinedKeys as $key) {


            if (isset($array1[$key]) && isset($array2[$key])) {

                if ((is_array($array1[$key]) || $array1[$key] instanceof AssociativeArray) && (is_array($array2[$key]) || $array2[$key] instanceof AssociativeArray)) {
                    $mergedArray[$key] = ArrayUtils::recursiveMerge($array1[$key], $array2[$key]);
                } else {
                    $mergedArray[$key] = $array1[$key];
                }

            } else {
                $mergedArray[$key] = isset($array1[$key]) ? $array1[$key] : $array2[$key];
            }

        }


        return $mergedArray;
    }
public static function recursiveMerge($array1, $array2) {

        if ($array1 instanceof AssociativeArray) {
            $array1 = $array1->toArray();
        }

        if ($array2 instanceof AssociativeArray) {
            $array2 = $array2->toArray();
        }

        if (ArrayUtils::isAssociative($array1) != ArrayUtils::isAssociative($array2)) {

            if (ArrayUtils::isAssociative($array1)) {
                $array1 = array($array1);
            } else {
                $array2 = array($array2);
            }
        }

        $combinedKeys = array_unique(array_merge(array_keys($array1), array_keys($array2)));

        $mergedArray = array();
        foreach ($combinedKeys as $key) {


            if (isset($array1[$key]) && isset($array2[$key])) {

                if ((is_array($array1[$key]) || $array1[$key] instanceof AssociativeArray) && (is_array($array2[$key]) || $array2[$key] instanceof AssociativeArray)) {
                    $mergedArray[$key] = ArrayUtils::recursiveMerge($array1[$key], $array2[$key]);
                } else {
                    $mergedArray[$key] = $array1[$key];
                }

            } else {
                $mergedArray[$key] = isset($array1[$key]) ? $array1[$key] : $array2[$key];
            }

        }


        return $mergedArray;
    }
public static function recursiveMerge ( $ array1 , $ array2 ) { if ( $ array1 instanceof AssociativeArray ) { $ array1 = $ array1 -> toArray ( ) ; } if ( $ array2 instanceof AssociativeArray ) { $ array2 = $ array2 -> toArray ( ) ; } if ( ArrayUtils :: isAssociative ( $ array1 ) != ArrayUtils :: isAssociative ( $ array2 ) ) { if ( ArrayUtils :: isAssociative ( $ array1 ) ) { $ array1 = array ( $ array1 ) ; } else { $ array2 = array ( $ array2 ) ; } } $ combinedKeys = array_unique ( array_merge ( array_keys ( $ array1 ) , array_keys ( $ array2 ) ) ) ; $ mergedArray = array ( ) ; foreach ( $ combinedKeys as $ key ) { if ( isset ( $ array1 [ $ key ] ) && isset ( $ array2 [ $ key ] ) ) { if ( ( is_array ( $ array1 [ $ key ] ) || $ array1 [ $ key ] instanceof AssociativeArray ) && ( is_array ( $ array2 [ $ key ] ) || $ array2 [ $ key ] instanceof AssociativeArray ) ) { $ mergedArray [ $ key ] = ArrayUtils :: recursiveMerge ( $ array1 [ $ key ] , $ array2 [ $ key ] ) ; } else { $ mergedArray [ $ key ] = $ array1 [ $ key ] ; } } else { $ mergedArray [ $ key ] = isset ( $ array1 [ $ key ] ) ? $ array1 [ $ key ] : $ array2 [ $ key ] ; } } return $ mergedArray ; }
Recursively merge array1 and array2.  If keys exist in both arrays at any level, take the value from array1 in preference.

@param $array1
@param $array2
Recursively merge array1 and array2 . If keys exist in both arrays at any level take the value from array1 in preference .