【发布时间】:2021-05-28 22:29:09
【问题描述】:
我正在使用 Laravel 8 和 PHP 8。
我有一个变量,我在全局范围内定义并分配了null 值。
我正在使用 laravel 集合的 each 函数来迭代每个元素。在这个闭包内我有另一个闭包,它是集合的filter 函数。我将过滤器function 的输出分配给全局变量,它正在工作,但仅在each 函数的闭包内。 (使用 xdebug 检查)
在闭包之外,变量再次变为null,即使我使用use()函数在闭包内传递了变量,但它仍然不起作用。
$filtered = null;
$product->each(function ($price) use($coupons, $filtered) {
$filtered = $coupons->filter(function ($coupon) use($price) {
if (!empty($price->iso_code)) {
if ($coupon->iso_code = $price->iso_code) {
$a = $price->currency_code;
$b = $coupon->currency_code;
return $a == $b;
}
}
return $price->currency_code = $coupon->currency_code;
});
});
return $filtered; //null
我已经推荐了this answer 和this also,但没有运气。
有人可以指导我如何将filter 函数的输出分配给全局变量吗?
【问题讨论】: