Is it just me, or is this function just array_merge_recursive??

function MergeConfig(Array $aConfig, Array $aVal)
    {
        $aReturn = [];
        foreach( $aVal as $k => $v )
        {
            if( isset($aConfig[$k]) && is_array($aConfig[$k]) && is_array($v) )
            {
                $aReturn[$k] = MergeConfig($aConfig[$k], $v);
            }
            else
            {
                $aReturn[$k] = $v;
            }
        }
        foreach( $aConfig as $k => $v )
        {
            if( !isset($aReturn[$k]) )
            {
                $aReturn[$k] = $v;
            }
        }

    return $aReturn;
}

    yes, except that it does not convert non-array indexes into arrays. I use a similar function in my config class. Given two strings with the same (string) index, [man]array_merge_recursive[/man] will create an array containing both values. This function will replace the value from the first array with the value from the second (which is, in my experience, almost always the desired behavior), but still recurse when the index is already an array.

    Give it a try:

    $a1 = ["a"=>"A","b"=>"B",'c'=>[1,2,3]];
    $a2 = ["a"=>"Z",'c'=>[4,5],'d'=>"D"];
    
    var_dump( array_merge_recursive( $a1,$a2 ) );
    /*
    array(4) {
      ["a"]=>
      array(2) {
        [0]=>
        string(1) "A"
        [1]=>
        string(1) "Z"
      }
      ["b"]=>
      string(1) "B"
      ["c"]=>
      array(5) {
        [0]=>
        int(1)
        [1]=>
        int(2)
        [2]=>
        int(3)
        [3]=>
        int(4)
        [4]=>
        int(5)
      }
      ["d"]=>
      string(1) "D"
    }
    */
    var_dump( MergeConfig( $a1,$a2 ) );
    /*
    array(4) {
      ["a"]=>
      string(1) "Z"
      ["c"]=>
      array(3) {
        [0]=>
        int(4)
        [1]=>
        int(5)
        [2]=>
        int(3)
      }
      ["d"]=>
      string(1) "D"
      ["b"]=>
      string(1) "B"
    }
    */

    edit

    there's an example in the manual (comments) here.

    The other difference here is that it doesn't take numeric indexes into account like array_merge_recursive does, but that's easy enough to fix.

      Oh, so then its like [man]array_replace_recursive[/man]

        Well… with this example, yeah, I guess so. This is more simplistic than I thought it was.

        Take these arrays as an example:

        $a1 = array(
          "one"=>"hello",
          "two" => array( "hello" );
        );
        $a2 = array(
          "one"=>"goodbye",
          "two" => array( "goodbye" )
        );

        with array_merge_recursive, you'll get:

        array(
          "one" => array( "hello","goodbye" ),
          "two" => array( "hello","goodbye" )
        );

        with array_replace_recursive:

        array(
          "one" => "goodbye",
          "two" => array( "goodbye" )
        );

        …what I would want (in many cases, but especially for merging config arrays) would be something like this:

        array(
          "one" => "goodbye",
          "two" => array( "hello","goodbye" )
        );

        Beyond my specific use case, it just seems more sensible to me. Here's the code I use to do that:

            mergeRecursive( array $array1,array $array2 ){
                $merged = $array1;
                foreach( $array2 as $key => $value ){
                    if( isset( $merged[$key] ) ){
                        if( is_array( $value ) && is_array( $merged[$key] ) ){
                            $merged[$key] = mergeRecursive( $merged[$key], $value );
                        }
                        elseif( is_int( $key ) ){ $merged[] = $value; }
                        else{ $merged[$key] = $value; }
                    }
                    else{
                        $merged[$key] = $value;
                    }
                }
                return $merged;
            }
          Write a Reply...