36

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?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Joshc
  • 3,825
  • 16
  • 79
  • 121
  • Seems good. Have you tried your `var_dump($gallery_location); var_dump($gallery_studio);` just before your `array_merge` ? – zessx Oct 31 '13 at 16:47
  • @zessx - I've just updated my question, it's becuase one of the arrays are empty which cause merge to return null overall :/ – Joshc Oct 31 '13 at 16:48

2 Answers2

78

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

Geoffrey Hale
  • 10,597
  • 5
  • 44
  • 45
zessx
  • 68,042
  • 28
  • 135
  • 158
  • This does not seem to result the null issue, please see my question above at the bottom, I have dumped everything so you can see what empty arrays are returning. Thanks for your help so far. – Joshc Oct 31 '13 at 17:14
  • There was a typo (`downlads` instead of `downloads`), but it should work, [see this PHP sandbox](http://sandbox.onlinephpfunctions.com/code/7512b59a0e08de604fcb1a87e8ba68d2fb1e57f3) – zessx Oct 31 '13 at 17:43
  • I love you so much dude this now works - sorry I was concentration, it was a cheap copy and paste on my behalf. Thanks for you help man. +1 – Joshc Oct 31 '13 at 18:03
  • Adding `(array)` is definitely the nicer way to handle this +1 – AO_ Feb 09 '16 at 10:49
  • 2
    Just a note, since the `?:` operator is available now, one can also do `array_merge($a1 ?: [], $a2 ?: [], $a3 ?: [], $a4 ?: []);` which is essentially the same as casting to `(array)` but a bit less typing – billrichards May 17 '21 at 15:40
4

You can use the following way for merger your arrays:

$c = (array)$a + (array)$b
slfan
  • 8,950
  • 115
  • 65
  • 78