Merge array only in empty values

1

I have a function that takes an array as an parameter, for example:

$args = array(
    "foo" => "value"
);
oneFunction( $args );

function oneFunction( $array ){
    $default = array(
        "foo" => "foo",
        "bar" => "bar"
    );
    //mescla valores
    $array = mesclaArrays($default, $array);

    //nesse exemplo, a saída deve ser
    //array(
    //  "foo" => "value",
    //  "bar" => "bar"
    //);
}

Is there a native function for this or do I need to create my own?

    
asked by anonymous 17.03.2015 / 13:05

1 answer

1

You can do the opposite and insert in the $default what comes from outside with a loop.

function oneFunction( $array ){
    $default = array(
        "foo" => "foo",
        "bar" => "bar"
    );
    //mescla valores
    foreach ($array as $key => $value) $default[$key] = $value;
    return $default
}

In this way you add new chave -> valor and / or existing keys with the new value .

    
17.03.2015 / 13:09