php 警告:array_merge():参数 #2 不是数组警告:array_merge():参数 #1 不是数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37347205/
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
Warning: array_merge(): Argument #2 is not an array Warning: array_merge(): Argument #1 is not an array
提问by Megan Fox
Please take a look to my code below .
请看看我下面的代码。
$referenceTable = array();
$referenceTable['val1'] = array(1, 2);
$referenceTable['val2'] = 3;
$referenceTable['val3'] = array(4, 5);
$testArray = array();
$testArray = array_merge($testArray, $referenceTable['val1']);
var_dump($testArray);
$testArray = array_merge($testArray, $referenceTable['val2']);
var_dump($testArray);
$testArray = array_merge($testArray, $referenceTable['val3']);
var_dump($testArray);
I was trying to work with two arrays as you can see and while trying to merge the empty array with the older ones i am getting the warnings as
正如您所看到的,我试图使用两个数组,在尝试将空数组与旧数组合并时,我收到了警告
Warning: array_merge(): Argument #2 is not an array
Warning: array_merge(): Argument #1 is not an array
The Output which i get is
我得到的输出是
array(2) { [0]=> int(1) [1]=> int(2) }
NULL
NULL
I am unable to fix this thing , help appreciated .
我无法解决这个问题,感谢帮助。
回答by AbraCadaver
All arguments passed to array_merge()
need to be arrays and $referenceTable['val2']
is not an array it is integer 3
. You can cast it to an array:
传递给的所有参数都array_merge()
必须是数组,$referenceTable['val2']
而不是数组,而是整数3
。您可以将其转换为数组:
$testArray = array_merge($testArray, (array)$referenceTable['val2']);
回答by Sofiene Djebali
$referenceTable['val2']
is an int not an array, declare $referenceTable like this to be array :
$referenceTable['val2']
是 int 而不是数组,像这样声明 $referenceTable 为数组:
PHP
PHP
$referenceTable['val2'] = [3];
that should work.
那应该工作。
回答by Machavity
Breaking this down
打破这个
$testArray = array_merge($testArray, $referenceTable['val2']);
The problem here is that
这里的问题是
$referenceTable['val2'] = 3;
3
is not an array. Set it to an array and it works
3
不是数组。将它设置为一个数组,它的工作原理
$referenceTable['val2'] = array(3);
As to why this fails
至于为什么会失败
$testArray = array_merge($testArray, $referenceTable['val3']);
You ran the previous statement, which set $testArray
to NULL
, which is also not an array
您运行了前面的语句,该语句设置$testArray
为NULL
,这也不是数组
回答by Japhet Johnson
Ok, I was able to resolve this issue and it occurred when I tried to do composer update. The command did not work and it only corrupted the Service.json file in
好的,我能够解决这个问题,它发生在我尝试进行作曲家更新时。该命令不起作用,它只损坏了 Service.json 文件
/var/www/html/app/storage/meta
So what you need to do is to delete the service.json file and replace it with the new one or you do the composer install command.
所以你需要做的是删除 service.json 文件并用新的替换它,或者你执行 composer install 命令。
The website now runs well on https://www.codemint.net
该网站现在在https://www.codemint.net上运行良好
回答by Dhivya Rajagopal
$referenceTable['val2']
is not an array it is integer 3.
$referenceTable['val2']
不是数组,它是整数 3。
You can cast into an array or multiple arrays:
您可以转换为一个数组或多个数组:
$testArray = array_merge($testArray, array($referenceTable['val2']));