php 如果一个或多个数组为空,merge_array 是否返回 null?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19711491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:54:46  来源:igfitidea点击:

merge_array returns null if one or more of arrays is empty?

phparraysadvanced-custom-fields

提问by Joshc

I will give you quick run down of what I am doing.

我会让你快速了解我在做什么。

I am using wordpress with the advanced custom fieldsplugin. This is a php based question because these get_field()fields contain object arrays.

我正在使用带有高级自定义字段插件的wordpress 。这是一个基于 php 的问题,因为这些get_field()字段包含对象数组。

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

For example $gallery_locationwhen dumped will return this...

例如,$gallery_location当 dumped 将返回此...

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...

然后我使用 merge_array 来合并两个对象......

$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!

我正在合并多个数组,但如果其中一个数组为空,那么这将导致合并数组完全返回 null!

My question is how can I stop merge_array returning null is some of the arrays are empty?

我的问题是如何停止 merge_array 返回 null 是一些数组是空的?

Thanks in advance for any ideas.

提前感谢您的任何想法。



@zessx

@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 $downloadsis still returning null, if I try and use both your solution below it does not work?

如您所见$downloads,仍然返回空值,如果我尝试使用下面的两种解决方案,它不起作用吗?

回答by zessx

array_mergeonly accepts arrays as parameters. If one of your parameters is null, it will raise an error :

array_merge只接受数组作为参数。如果您的参数之一为空,则会引发错误:

Warning: array_merge(): Argument #x is not an array...

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

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

1/ 强制类型为 array

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

2/ Check if variables are arrays

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);

PHP Sandbox

PHP 沙盒

回答by Константин Дмитриенко

You can use the following way for merger your arrays:

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

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