2 回答

TA貢獻1829條經驗 獲得超9個贊
我認為您可以使用回調根據文件大小對文件進行排序。也許是這樣的東西,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貢獻1853條經驗 獲得超18個贊
循環文件時保持最?。?/p>
$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 關注
- 182 瀏覽