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

江枫思渺然 提交于 2019-12-09 07:46:29

问题


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_field() fields contain object arrays.

$gallery_location   = get_field('gallery_location');
$gallery_studio = get_field('gallery_studio');

For example $gallery_location when dumped will return this...

array(18) {
  [0]=>
  array(10) {
    ["id"]=>
    int(126)
    ["alt"]=>
    string(0) ""
    ["title"]=>
    string(33) "CBR1000RR STD Supersport 2014 001"
    ["caption"]=>
    string(0) ""
    ["description"]=>
    string(0) ""
    ["mime_type"]=>
    string(10) "image/jpeg"
    ["url"]=>
    string(94) "http://www.example.com/wp/wp-content/uploads/2013/10/CBR1000RR-STD-Supersport-2014-001.jpg"
    ["width"]=>
    int(7360)
    ["height"]=>
    int(4912)
  }
... on so fourth
}

I am then using merge_array to merge both objects...

$gallery_location = get_field('gallery_location');
$gallery_studio = get_field('gallery_studio');

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

I am merging multiple arrays but if one of the arrays is empty then this is causing the merge array to return null entirely!

My question is how can I stop merge_array returning null is some of the arrays are empty?

Thanks in advance for any ideas.


@zessx

This is what I am returning...

$gallery_location   = get_field( 'gallery_location' );
$gallery_studio     = get_field( 'gallery_studio' );

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

var_dump($gallery_location);

var_dump($gallery_studio);

var_dump($downloads);


and these are the results of dumps above in same order...

string(0) ""


array(18) {
  [0]=>
  array(10) {
    ["id"]=>
    int(126)
    ["alt"]=>
    string(0) ""
    ["title"]=>
    string(33) "CBR1000RR STD Supersport 2014 001"
    ["caption"]=>
    string(0) ""
    ["description"]=>
    string(0) ""
    ["mime_type"]=>
    string(10) "image/jpeg"
    ["url"]=>
    string(94) "http://www.example.com/wp/wp-content/uploads/2013/10/CBR1000RR-STD-Supersport-2014-001.jpg"
    ["width"]=>
    int(7360)
    ["height"]=>
    int(4912)
  }
... on so fourth
}


NULL


As you can see $downloads is still returning null, if I try and use both your solution below it does not work?


回答1:


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




回答2:


You can use the following way for merger your arrays:

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


来源:https://stackoverflow.com/questions/19711491/merge-array-returns-null-if-one-or-more-of-arrays-is-empty

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