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

前端 未结 2 1551
感动是毒
感动是毒 2021-02-05 03:08

I will give you quick run down of what I am doing.

I am using wordpress with the advanced custom fields plugin. This is a php based question because these get_fiel

相关标签:
2条回答
  • 2021-02-05 03:51

    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

    0 讨论(0)
  • 2021-02-05 04:00

    You can use the following way for merger your arrays:

    $c = (array)$a + (array)$b
    
    0 讨论(0)
提交回复
热议问题