fork(1) download
  1. <?php
  2. $a = array(
  3. 'a' => NULL,
  4. 'b' => 1,
  5. 'c' => 1
  6. );
  7.  
  8. $b = array(
  9. 'a' => 1,
  10. 'b' => NULL,
  11. 'c' => 1
  12. );
  13.  
  14. function arrayMergeIgnoringNull($arr1, $arr2) {
  15. $new2 = array();
  16. forEach ($arr2 as $key => $value) {
  17. if (($value !== NULL) || !isSet($arr1[$key])) {
  18. $new2[$key] = $value;
  19. }
  20. }
  21.  
  22. return array_merge($arr1, $new2);
  23. }
  24.  
  25. $c = arrayMergeIgnoringNull($a, $b);
  26. print_r($c);
  27. ?>
  28.  
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Array
(
    [a] => 1
    [b] => 1
    [c] => 1
)