当通过函数创建数组时,我正在尝试合并它
我有一个函数,它返回一个数组。
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;
}
}
为什么我试图将数组合并在一起:
$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?
它说它不是一个数组?
array_merge() [function.array-merge]: Argument #1 is not an array
但我可以print_r($arrayAr);
,它就像while循环中的一个数组,但它不显示它之外的任何内容?
当我尝试这个。。。
$new_array = array_merge((array)$arrayAr ,(array)$array3);
它没有显示错误,但也没有合并。
帮助?
感谢
您的$arrayAr
是局部变量,第二个print_r()
在其变量范围之外使用。如果您需要更广泛的可用性,请在foreach
之前"声明"它,即$arrayAr = array();
(或您想要的任何值——在这种情况下这并不重要,但array()
使代码变得清晰)。