Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP merge array on nulls

Tags:

arrays

merge

php

I have 2 * array and I want to merge them, but each of theme have some NULL rows.

$a = array(
    'a' => NULL,
    'b' => 1,
    'c' => 1
);

$b = array(
    'a' => 1,
    'b' => NULL,
    'c' => 1    
);

So, code:

$c = array_merge($a,$b);

Will give $c:

array {
  'a'=> 1
  'b'=> NULL
  'c'=>1
}

Is there build in or simple way to do margin ($a,$b) like following, but now $a is overwritten for every same index from $b. I want $b to be overwritten by $a index if $b index is null - in example $b['b'] should be overwritten from $a

All NULL rows should be filled if possible.

like image 758
Adam Pietrasiak Avatar asked Jun 03 '13 06:06

Adam Pietrasiak


2 Answers

I think you can use array_filter function to remove null values in both array and then merge them

$a = array(
    'a' => NULL,
    'b' => 1,
    'c' => 1
);

$b = array(
    'a' => 1,
    'b' => NULL,
    'c' => 1    
);

$b  = array_filter($b);
$a  = array_filter($a);
$c = array_merge($a, $b);
var_dump($c);

This will output

array(3) {
  ["b"]=> int(1)
  ["c"]=> int(1)
  ["a"]=> int(1)
}

LIVE SAMPLE

As side note i would add that using array_filter without second parameter will end up in deleting all NULL values as well as EMPTY array etc. If you want to only remove NULL values so you will need to use array_filter($yourarray, 'strlen');

EDITED

If you want to preserve NULL if both arrays have it with the same key and supposing that both arrays have same number of keys/values, then you will need to loop inside your array and build a new array preserving NULL where you need

$a = array(
    'a' => NULL,
    'b' => 1,
    'c' => 1,
    'd' => NULL
);

$b = array(
    'a' => 1,
    'b' => NULL,
    'c' => 1,   
    'd' => NULL,
);

$c = array();
foreach($a as $key => $val)
{
    if($key == NULL && $b[$key] == NULL)
    {
        $c[$key] = $val;
    } else if($key != NULL && $b[$key] == NULL) {
        $c[$key]= $val;
    } else if($key != NULL && $b[$key] != NULL) {
        $c[$key]= $b[$key];
    } else {
        $c[$key]= $b[$key];
    }
}
var_dump($c);

This will output

array (size=4)
  'a' => int 1
  'b' => int 1
  'c' => int 1
  'd' => NULL

LIVE SAMPLE

like image 76
Fabio Avatar answered Oct 26 '22 23:10

Fabio


None of these answers address merging two arrays that may have different keys, which is what lead me to this SO post. It happens to actually be pretty straight forward fortunately:

function arrayMergeIfNotNull($arr1, $arr2) {
    foreach($arr2 as $key => $val) {
        $is_set_and_not_null = isset($arr1[$key]);
        if ( $val == NULL && $is_set_and_not_null ) {
            $arr2[$key] = $arr1[$key];
        }
    }
    return array_merge($arr1, $arr2);
}

Now, merging these two arrays:

$a = array('a' => NULL, 'b' => 1, 'c' => 1, 'd' => NULL, 'z' => 'zebra');
$b = array('a' => 1, 'b' => NULL, 'c' => 1, 'd' => NULL, 'f' => 'frank');

with:

var_dump(arrayMergeIfNotNull($a, $b));

will produce:

array (size=6)
  'a' => int 1
  'b' => int 1
  'c' => int 1
  'd' => NULL
  'z' => 'zebra'
  'f' => 'frank'

Note this answer also solves the problem of two arrays with the same keys.

like image 33
jbmilgrom Avatar answered Oct 26 '22 23:10

jbmilgrom