2 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊
我認(rèn)為您可以使用回調(diào)根據(jù)文件大小對文件進(jìn)行排序。也許是這樣的東西,sortBy
//after Arr::where
$fileWithSmallestSize = collect($files)->sortBy(function($file){
return Storage::size($file);
})->first();
另一種選擇是減少收集。
//after Arr::where
$fileWithSmallestSize = collect($files)->reduce(function($smallestFile, $file){
return ( Storage::size($file) < Storage::size($smallestFile) )? $file : $smallestFile;
}, $files[0]);

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊
循環(huán)文件時(shí)保持最小:
$files = Storage::files("videos");
$files = Arr::where($files, function ($value, $key) {
return Str::contains($value, 'string..') && Str::endsWith($value ,'.mp4');
}); // keep only certain files from videos directory in the files array
$minSize = PHP_INT_MAX;
$minFile = null;
foreach ($files as $key => $file)
{
$size = Storage::size($file);
if ($size < $minSize) {
$minSize = $size;
$minFile = $file;
}
$files[$key] = ['file' => $file, 'size' => $size];
}
// $minSize is the smallest size in the directory and $minFile is the file with the smallest size
// $minFile will be `null` if there are no files in the initial array, you should add a check for this possibility
- 2 回答
- 0 關(guān)注
- 177 瀏覽