如果一个或多个数组为空,则merge_array返回null? [英] merge_array returns null if one or more of arrays is empty?
问题描述
我将为您快速介绍我的工作。
I will give you quick run down of what I am doing.
我正在将wordpress与高级自定义字段插件。这是一个基于php的问题,因为这些 get_field()
字段包含对象数组。
I am using wordpress with the advanced custom fields plugin. This is a php based question because these get_field()
fields contain object arrays.
$gallery_location = get_field('gallery_location');
$gallery_studio = get_field('gallery_studio');
例如 $ gallery_location
会在转储时返回此...
For example $gallery_location
when dumped will return this...
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合并两个对象...
I am then using merge_array to merge both objects...
$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!
我的问题是如何停止merge_array返回null,因为某些数组为空?
My question is how can I stop merge_array returning null is some of the arrays are empty?
预先感谢您的任何想法。
Thanks in advance for any ideas.
@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
您可以看到 $ downloads
仍然返回null,如果我尝试同时使用下面的两个解决方案都行不通?
As you can see $downloads
is still returning null, if I try and use both your solution below it does not work?
推荐答案
array_merge
仅接受数组作为参数。如果您的参数之一为null,则会引发错误:
array_merge
only accepts arrays as parameters. If one of your parameters is null, it will raise an error :
警告:array_merge():参数#x不是数组。 ..
Warning: array_merge(): Argument #x is not an array...
如果其中一个数组为空,则不会引发此错误。空数组仍然是数组。
This error won't be raised if one of the arrays is empty. An empty array is still an array.
两个选项:
1 /强制类型为是 array
1/ Force the type to be 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);
这篇关于如果一个或多个数组为空,则merge_array返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!