我会简单介绍一下我的工作内容。
我正在使用 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,如果我尝试同时使用下面您提供的解决方案它不能工作?
array_merge
前面尝试过var_dump($gallery_location); var_dump($gallery_studio);
吗? - zessx