I need to split an array up evenly or as close as possible. E.g. I have 128 items in an array, I need to split it up to show on 3 pages, how do it while keeping the remainder 8 on page 3?
You'll have 42 items on pages 1 and 2 and 44 items on page 3. for($i = 0; $i < 3; $i++) for($j = 0; $j < 42; $j++) { //do what you want on each page here } //do what you want to go to the next page here } PHP:
$arr = Array( /* YOUR ITEMS HERE */ ); //assumed $arr = array_chunk($arr, 110); //so the last chunk has 8 items $page = intval($_GET['page']); #assumed getting page id from GET if ($page <= 0 or $page > count($arr)) $page = 1 #now display the array chunk for this page print_r( $arr[--$page]; ); PHP: I did not test but I am certain it works. Stay well....
Let me correct you Sir. There's absolutely no need to use "foreach" loop, as "array_chunk" splits the array among chunks of desired size.
Can you define 'keeping'? Are you simply saying split them up to show on one HTML send using some form of page break (in which case yes, array_chunk) or are you referring to holding onto the results for a separate HTML call/send? If the latter, how are you building that array?