【发布时间】:2013-11-11 18:03:32
【问题描述】:
我会让你快速了解我在做什么。
我正在使用带有advanced custom fields 插件的wordpress。这是一个基于 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 - 我刚刚更新了我的问题,这是因为其中一个数组是空的,导致合并返回 null 整体:/
标签: php arrays wordpress advanced-custom-fields