Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 701
Joshc Avatar asked Oct 31 '13 16:10

Joshc


2 Answers

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

like image 192
zessx Avatar answered Oct 20 '22 05:10

zessx


You can use the following way for merger your arrays:

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