0

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. Issue is that sometimes the number of images are just too many (30-500 images).

What i wanted to do is provide a summary of that array, by reducing that array to just 10 images. I could easily get the first 10 by limiting the loop. 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.

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?

1
  • I wanted to get 10 images from that array in a way that the 10 images is equally spread out throughout the day. - see, that sentence defines the logic. Now, instead of us figuring out what the author wanted to achieve, why don't you explain the algorithm in a proper, logical way using IT terminology? I'd say your question is unclear, but you're obviously doing two things - figuring out this algorithm I mentioned and thinking about efficiency. Focus on one, explain it clearly, show what you tried and where you got stuck. Commented Apr 14, 2017 at 12:36

2 Answers 2

2

There is no built-in function in php for do that. The best thing I think is using of sizeof() function:

$size = sizeof($array);
$chunkSize = ceil($size/10);
for($i = 0;$i<$size;$i+=$chunkSize){
    echo $array[$i];
}
Sign up to request clarification or add additional context in comments.

Comments

1
$images = ... //result from database
$result = [];

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.