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?
array_merge
only accepts arrays as parameters. If one of your parameters is null, it will raise an error :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
2/ Check if variables are arrays
PHP Sandbox
You can use the following way for merger your arrays: