public static function merge( $destination, $source, bool $deep = true, bool $assoc = false ) {

    if( !static::is( $destination, false, true ) ) throw new \TypeError( 'Destination must be array(like)' );
    else if( !static::is( $source, true ) ) throw new \TypeError( 'Source must be iterable' );
    else {

      // handle special case when both arrays are completly numeric
      if( !$assoc && static::isNumeric( $source ) && static::isNumeric( $destination ) ) {

        $index = -1;
        foreach( $destination as $tmp => $_ ) $index = max( $index, $tmp );
      }

      $result = $destination;
      foreach( $source as $key => $value ) {

        $tmp = isset( $index ) ? ++$index : $key;
        if( !$deep || !static::is( $value, true ) || !static::is( $result[ $tmp ] ?? null, true ) ) $result[ $tmp ] = $value;
        else $result[ $tmp ] = static::merge( $result[ $tmp ], $value, $deep );
      }
    }

    return $result;
  }
public static function merge( $destination, $source, bool $deep = true, bool $assoc = false ) {

    if( !static::is( $destination, false, true ) ) throw new \TypeError( 'Destination must be array(like)' );
    else if( !static::is( $source, true ) ) throw new \TypeError( 'Source must be iterable' );
    else {

      // handle special case when both arrays are completly numeric
      if( !$assoc && static::isNumeric( $source ) && static::isNumeric( $destination ) ) {

        $index = -1;
        foreach( $destination as $tmp => $_ ) $index = max( $index, $tmp );
      }

      $result = $destination;
      foreach( $source as $key => $value ) {

        $tmp = isset( $index ) ? ++$index : $key;
        if( !$deep || !static::is( $value, true ) || !static::is( $result[ $tmp ] ?? null, true ) ) $result[ $tmp ] = $value;
        else $result[ $tmp ] = static::merge( $result[ $tmp ], $value, $deep );
      }
    }

    return $result;
  }
public static function merge ( $ destination , $ source , bool $ deep = true , bool $ assoc = false ) { if ( ! static :: is ( $ destination , false , true ) ) throw new \ TypeError ( 'Destination must be array(like)' ) ; else if ( ! static :: is ( $ source , true ) ) throw new \ TypeError ( 'Source must be iterable' ) ; else { // handle special case when both arrays are completly numeric if ( ! $ assoc && static :: isNumeric ( $ source ) && static :: isNumeric ( $ destination ) ) { $ index = - 1 ; foreach ( $ destination as $ tmp => $ _ ) $ index = max ( $ index , $ tmp ) ; } $ result = $ destination ; foreach ( $ source as $ key => $ value ) { $ tmp = isset ( $ index ) ? ++ $ index : $ key ; if ( ! $ deep || ! static :: is ( $ value , true ) || ! static :: is ( $ result [ $ tmp ] ?? null , true ) ) $ result [ $ tmp ] = $ value ; else $ result [ $ tmp ] = static :: merge ( $ result [ $ tmp ] , $ value , $ deep ) ; } } return $ result ; }
Recursive merge of two arrays

This is like the array_merge_recursive() without the strange array-creating thing

@param array|object $destination
@param array|object $source
@param bool         $deep
@param bool         $assoc Handle numeric arrays like associative (overwrite keys, not extend)

@return array|object The extended destination
@throws \TypeError
Recursive merge of two arrays