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

折月煮酒 提交于 2019-12-03 09:39:37
zessx

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!