merge_array returns null if one or more of arrays is empty? merge_array returns null if one or more of arrays is empty? wordpress wordpress

merge_array returns null if one or more of arrays is empty?


array_merge only accepts arrays as parameters. If one of your parameters is null, it will raise an error :

Warning: array_merge(): Argument #x is not an array...

This error won't be raised if one of the arrays is empty. An empty array is still an array.

Two options :

1/ Force the type to be array

$downloads = array_merge( (array)$gallery_location, (array)$gallery_studio );

2/ Check if variables are arrays

$downloads = array();if(is_array($gallery_location))    $downloads = array_merge($downloads, $gallery_location);if(is_array($gallery_studio ))    $downloads = array_merge($downloads, $gallery_studio);

PHP Sandbox


You can use the following way for merger your arrays:

$c = (array)$a + (array)$b