我想返回chunk的结果。问题是,当我使用foreach进行迭代时,我输入echo result它会显示结果但当我想返回结果时,会出现空白页
$tab = array();
Product::blabla ->chunk (500, function($results))
{
foreach($results as $result)
{
array_push ($tab,$result);
echo $results;// works
return $results;// doesn't return anything
}
}
return $tab; // to be sent to Ajax type get
闭包可以存储到变量中,所以只需添加到变量中。
$tab = Product::blabla()->chunk (500, function($results))
{
// your logic
return $results;
};
return $tab
这实际上不起作用,它只是返回true。您必须使闭包函数使用外部变量作为引用示例:
$output = [];
$tab = Product::blabla()->chunk (500, function($results) use (&$output))
{
$output = array_combine($output, $results);
};
return $output;