[英]PHP array merge: is not an array?
I'm tryin to merge an array that I have when it is being created through a function 我正在尝试合并通过函数创建时所拥有的数组
I have a function, and it returns an array. 我有一个函数,它返回一个数组。
class myarray
{
public function getAr($id)
{
// mysql query
while($dd= $database->fetch(PDO::FETCH_ASSOC))
{
$data[] = $dd; //there's values in the array when its being populated through the function of the while loop
}
return $data;
}
public function get3($id)
{
// mysl query
while($dd= $database->fetch(PDO::FETCH_ASSOC))
{
$data[] = $dd; //there's values in the array when its being populated through the function of the while loop
}
return $data;
}
}
How come I tried to merge together the array: 我如何尝试将数组合并在一起:
$get = new myarray();
while($row = $fet->fetch(PDO::FETCH_ASSOC))
{
$arrayAr = $get->getAr($id);
$array3 = $get->get3($id);
$new_array = array_merge($arrayAr ,$array3); //this gives me the error
print_r($arrayAr); //displays array
}
print_r($arrayAr); //displays nothing, why is that?
It says that its not an array? 它说不是数组吗?
array_merge() [function.array-merge]: Argument #1 is not an array
But I can print_r($arrayAr);
但是我可以
print_r($arrayAr);
and its like an array inside the while loop, but it doesn't display anything outside of it? 它就像while循环中的数组,但是它不在外面显示任何内容?
when I tried this... 当我尝试这个...
$new_array = array_merge((array)$arrayAr ,(array)$array3);
It doesn't display an error, but it isn't merged either. 它不会显示错误,但是也不会合并。
Help? 救命?
Thanks 谢谢
You $arrayAr
is local variable and 2nd print_r()
is used beyond of scope of his variable. $arrayAr
是局部变量,第二个print_r()
的使用超出了他的变量范围。 If you need it available wider, "declare" it before foreach
, with ie $arrayAr = array();
如果需要更广泛的使用,请在
foreach
之前“声明”,即$arrayAr = array();
(or whatever value you want - it is not important in this case, yet array()
makes code clear). (或您想要的任何值-在这种情况下并不重要,但
array()
使代码清晰可见)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.