Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果一个或多个数组为空,则merge_数组返回null?_Php_Arrays_Wordpress_Advanced Custom Fields - Fatal编程技术网

Php 如果一个或多个数组为空,则merge_数组返回null?

Php 如果一个或多个数组为空,则merge_数组返回null?,php,arrays,wordpress,advanced-custom-fields,Php,Arrays,Wordpress,Advanced Custom Fields,我会给你简要介绍一下我正在做的事情 我正在使用wordpress和插件。这是一个基于php的问题,因为这些get\u field()字段包含对象数组 $gallery_location = get_field('gallery_location'); $gallery_studio = get_field('gallery_studio'); 例如,转储时,$gallery\u location将返回此 array(18) { [0]=> array(10) { ["

我会给你简要介绍一下我正在做的事情

我正在使用wordpress和插件。这是一个基于php的问题,因为这些
get\u field()
字段包含对象数组

$gallery_location   = get_field('gallery_location');
$gallery_studio = get_field('gallery_studio');
例如,转储时,
$gallery\u 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_数组合并两个对象

$gallery_location = get_field('gallery_location');
$gallery_studio = get_field('gallery_studio');

$downloads = array_merge( $gallery_location, $gallery_studio );
我正在合并多个数组,但如果其中一个数组为空,则这将导致合并数组完全返回null

我的问题是,如果某些数组为空,如何停止merge_数组返回null

提前谢谢你的建议


@泽克斯

这就是我要回来的

$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) ""




如您所见,
$downloads
仍返回null,如果我尝试使用下面的两种解决方案,它是否不起作用

array\u merge
只接受数组作为参数。如果其中一个参数为null,则会引发错误:

警告:数组_merge():参数#x不是数组

如果其中一个数组为空,则不会引发此错误。空数组仍然是数组

两种选择:

1/强制类型为
array

$downloads = array_merge( (array)$gallery_location, (array)$gallery_studio );
2/检查变量是否为数组

$downloads = array();
if(is_array($gallery_location))
    $downloads = array_merge($downloads, $gallery_location);
if(is_array($gallery_studio ))
    $downloads = array_merge($downloads, $gallery_studio);

您可以使用以下方法合并阵列:

$c = (array)$a + (array)$b

看起来不错。您是否尝试过
var\u转储($gallery\u location);var_dump($gallery_studio)
就在你的
数组\u merge
?@zessx-我刚刚更新了我的问题,因为其中一个数组是空的,这会导致merge整体返回null:/这似乎不会导致null问题,请查看上面底部的我的问题,我已经转储了所有内容,以便你可以看到空数组返回的内容。谢谢你迄今为止的帮助。有一个打字错误(
downlads
而不是
下载
),但它应该可以工作,我太爱你了,伙计,现在可以工作了-很抱歉我太专注了,这是一个便宜的复制和粘贴,代表我。谢谢你的帮助+1添加
(数组)
无疑是处理此问题的更好方法+1只是一个注释,因为
?:
操作符现在可用,您也可以执行
数组合并($a1?:[],$a2?:[],$a3?:[],$a4?:[])
基本上与强制转换到
(数组)
相同,但键入的次数要少一点
$downloads = array();
if(is_array($gallery_location))
    $downloads = array_merge($downloads, $gallery_location);
if(is_array($gallery_studio ))
    $downloads = array_merge($downloads, $gallery_studio);
$c = (array)$a + (array)$b