简体   繁体   English

PHP:将大型数组汇总为10个数组的最有效方法是什么?

[英]PHP: What's the most efficient way to summarize a large array into just 10 arrays?

My web app returns to the user all images in a specific folder (pictures taken for each day in the month) via array of filenames. 我的Web应用通过文件名数组将特定文件夹中的所有图像(一个月中每天的照片)返回给用户。 Issue is that sometimes the number of images are just too many (30-500 images). 问题是有时图像数量太多(30-500张图像)。

What i wanted to do is provide a summary of that array, by reducing that array to just 10 images. 我想做的是通过将阵列减少到仅10张图像来提供该阵列的摘要。 I could easily get the first 10 by limiting the loop. 我可以通过限制循环轻松获得前10个。 But that just represents the first part (for few images of the day) . 但这仅代表第一部分(仅用于当天的少量图像)。 I wanted to get 10 images from that array in a way that the 10 images is equally spread out throughout the day. 我想从该阵列中获取10张图像,以使10张图像全天平均分布。

I could think of several ways to do this but involving quite a lot of code. 我可以想到几种方法来做到这一点,但是要涉及很多代码。

Was wondering if anyone knows of a cool array function or method that solves this problem? 想知道是否有人知道一个很酷的数组函数或方法可以解决这个问题?

There is no built-in function in php for do that. php中没有内置函数可以做到这一点。 The best thing I think is using of sizeof() function: 我认为最好的事情是使用sizeof()函数:

$size = sizeof($array);
$chunkSize = ceil($size/10);
for($i = 0;$i<$size;$i+=$chunkSize){
    echo $array[$i];
}
$images = ... //result from database
$result = [];

$total = count($images);
$index = round($total/10);

for($i = 0; $i < $total; $i += $index) {
    $result[] = $images[$i];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM