Jump to content

Read every 5 entries in an array into sub arrays


hp1

Recommended Posts

Hi,Been ages since I fiddled with one of my sites. Its a simple gallery that shows thumbnails as subsets of 5 images that can be scrolled through 5 at a time.I've managed to read all of the images in the directory into an array stripping out . and .. but now I'd like to iterate through that array taking each 5 images into a new array something like

$imagearray= array(image1, image2.........image 10)some code here that give me.....$sub1=array(image1, image2.....image5);$sub2=array(image6, image7.....image10);

This will the allow me to use the images in groups of 5.I suspect this is easier than I think it is, i just cant figure it....Appreciate any tips.cheers

Link to comment
Share on other sites

Shamefully I have nothing for this little problem other than existing array thats holding all the images. Needing a loop was about as far as i'd got.... I cant figure out the code that will then slice that array up into 5s into separate smaller arrays.

Link to comment
Share on other sites

You can use array_chunk to return an array of smaller arrays:http://www.php.net/manual/en/function.array-chunk.phpe.g.:

$groups = array_chunk($images, 5);echo count($groups) . ' groups of images';for ($i = 0; $i < count($groups); $i++){  print_r($groups[$i]);}

You can also use array_slice to get a piece of an array:http://www.php.net/manual/en/function.array-slice.php

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...