5

I was wondering if there was a function that would merge two or more arrays but would ignore any key value which is not contained with in the first/base array.

Here is a quick example of what I am doing with the current result and the result I am looking for.

<?php

$array1 = array('a' => 1, 'b' => 2);
$array2 = array('b' => 3, 'c' => 4);
$result = array_merge($array1, $array2);

// current result
// $result = array('a' => 1,'b' => 3, 'c' => 4);

// what i would like
// $result = array('a' => 1,'b' => 3);

?>
cmorrissey
  • 8,493
  • 2
  • 23
  • 27
  • I don't know of anything built in, but it would be easy to make your own. – Jonathan Kuhn Feb 06 '15 at 21:12
  • @JonathanKuhn I was going through all the documentation and couldn't find what I was looking for, thanks for confirming – cmorrissey Feb 06 '15 at 21:13
  • 2
    Unfortunately [`array_replace()`](http://php.net/manual/en/function.array-replace.php) creates newly found elements. Otherwise, it would work exactly. Should be a flag to disable this option IMO. – Jason McCreary Feb 06 '15 at 21:21

3 Answers3

7

The request "ignore any key value which is not contained with in the first/base array" calls for array_intersect_key()

$array1 = array('a' => 1, 'b' => 2);
$array2 = array('b' => 3, 'c' => 4);
$result = array_merge($array1, array_intersect_key($array2, $array1));

array_intersect_key($array2, $array1) compares the keys of $array2 and $array1 and keeps the values from $array2 that are associated with the keys that are common to both arrays.

Ian Dunn
  • 3,541
  • 6
  • 26
  • 44
axiac
  • 68,258
  • 9
  • 99
  • 134
  • Was going to post exact same thing, but mention that it is only for 2 arrays. – AbraCadaver Feb 06 '15 at 21:18
  • exactly what I was looking to do, thanks for pointing me in the right direction, don't know why I was stumbling over this – cmorrissey Feb 06 '15 at 21:20
  • @AbraCadaver nothing stops you to add how many extra `array_intersect_key($arrayN, $array1)` you want in the call to `array_merge()`. – axiac Feb 06 '15 at 21:27
1

Here is a quick example that I just wrote:

function array_merge_custom(){
    //get all the arguments
    $arrays = func_get_args();

    //get the first argument
    $first = array_shift($arrays);

    //loop over the first argument by key and value
    foreach($first as $key=>&$value){
        //loop over remaining arrays
        foreach($arrays as $otherArray){
            //check if key from first array exists in subsequent array
            if(array_key_exists($key, $otherArray)){
                //overwrite value
                $value = $otherArray[$key];
            }
        }
    }
    //return the first array with new values
    return $first;
}

http://codepad.viper-7.com/AE9rkV

Benefit of this is that it works for any number of arrays, not just 2.

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
0

I have encountered a similar problem myself. Here's my workaround :

<?php $array1 = array('a' => 1, 'b' => 2);
$array2 = array('b' => 3, 'c' => 4);
$result = array_intersect_key($array2, $array1) + $array1; ?>

Keep in mind you should respect the order if you want the second array with the higher priority.