PHP Merge Array with Zeros

I have a 2 * array and I want to merge them, but each of them has several NULL rows .

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

So the code:

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

Gives $ c:

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

Is there a built-in or easy way to make margin ($a,$b) next, but now $a overwritten for every index from $b . I want $b be overwritten by the index $ index, if $b index is NULL - in the example $b['b'] should be overwritten with $a

All NULL strings should be filled, if possible.

+7
source share
5 answers

I think you can use the array_filter function to remove null values ​​in both arrays and then combine 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 lead to the conclusion

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

LIVE SAMPLE

As a side note, I would add that using array_filter without a second parameter will delete all NULL values, as well as an EMPTY array, etc. If you want to remove only NULL values, you need to use array_filter($yourarray, 'strlen');

EDITED

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

 $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 lead to the conclusion

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

LIVING SAMPLE

+15
source

None of these answers relate to merging two arrays that can have different keys , which led me to this SO post. Fortunately, this is actually quite simple:

 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, merge 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'); 

from:

 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' 

Pay attention to this answer also solves the problem of two arrays with the same keys.

+1
source

then you need to pass $ b as the first parameter

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

this function can be used

 function mergeArray($array1, $array2) { $result = array(); foreach ($array1 as $key=>$value) { $result[$key] = $value; } foreach ($array2 as $key=>$value) { if (!isset($result[$key] || $result[$key] == null) { $result[$key] = $value; } } return $result; } 
0
source

If you need to store keys that are NULL in both arrays, you can use a custom function that ignores those entries from the second array if (and only if) there is a corresponding non-NULL in the first array. It will look like this:

 function arrayMergeIgnoringNull($arr1, $arr2) { $new2 = array(); forEach ($arr2 as $key => $value) { if (($value !== NULL) || !isSet($arr1[$key])) { $new2[$key] = $value; } } return array_merge($arr1, $new2); } $c = arrayMergeIgnoringNull($a, $b); 

See also this .

0
source

I would do this with a simple single liner:

 $c = array_filter($b) + array_filter($a) + array_fill_keys(array_keys($b),null); 

This will save all the keys and values ​​of $b , unless false, in which case they will either be replaced with the values ​​from $a , or as a last resort replaced with null .

When combining arrays with the + operator, the order in which they are displayed from left to right corresponds to priority. Thus, the leftmost position will hold this value if the key does not exist.

You can also reverse the order and use array_merge() , but for me it is more complicated before our eyes:

 $c = array_merge(array_fill_keys(array_keys($b),null) + array_filter($a) + array_filter($b)); 
0
source

All Articles