我会给你快速运行我所做的。如果一个或多个数组为空,merge_array返回null?
我使用wordpress和advanced custom fields插件。这是一个基于php的问题,因为这些get_field()
字段包含对象数组。
$gallery_location = get_field('gallery_location');
$gallery_studio = get_field('gallery_studio');
例如$gallery_location
倾倒时会返回此...
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
}
然后我使用merge_array合并这两个对象...
$gallery_location = get_field('gallery_location');
$gallery_studio = get_field('gallery_studio');
$downloads = array_merge($gallery_location, $gallery_studio);
我合并多个阵列,但如果其中一个数组为空,那么这会导致合并数组完全返回null!
我的问题是如何停止merge_array返回null是一些数组是空的?
在此先感谢您的任何想法。
@zessx
这就是我回来......
$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);
,这些都是上面相同的顺序堆放的结果...
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
正如你可以看到$downloads
仍然返回null,如果我尝试使用下面两个是你的解决方案不起作用?
看起来不错。你有没有尝试过'var_dump($ gallery_location); var_dump($ gallery_studio);'就在'array_merge'之前? – zessx
@zessx - 我刚刚更新了我的问题,这是因为其中一个数组是空的,这会导致合并全部返回null:/ – Joshc