如何获得关闭php内部的价值

S.M*_*ian 1 php laravel

我想在each我上面定义的函数闭包中得到一个变量.但它返回null:

public function saveSettingsImages(Collection $collection) : array
{
    $arrImages = Array();
    $collection->each(function (UploadedFile $file) use ($arrImages) {
        $filename = $file->store('sliders', ['disk' => 'public']);
        echo $filename; // it prints name of file as well
        array_push($arrImages,$filename);
    });
    dd($arrImages);//it is empty array
    return $arrImages;
}
Run Code Online (Sandbox Code Playgroud)

但是dd($arrImages);空的!

Sér*_*eis 5

尝试将引用传递给变量,而不是像

$collection->each(function (UploadedFile $file) use (&$arrImages) {
    $filename = $file->store('sliders', ['disk' => 'public']);
    echo $filename; // it prints name of file as well
    array_push($arrImages,$filename);
});
Run Code Online (Sandbox Code Playgroud)