Hello people! I'm a bit confused here. I have a php array like this Array(2010,2009,2008 ...1992) and i want to create a loop to print a menu with a four year range counting down like this 2010-2006 2005-2001 2000-1996 etc.. How can i do this> Everything i tried end up in an endless loop. THnx in advance. J.
+3
A:
foreach(array_chunk($years, 5) as $val) {
echo reset($val) . "-" . end($val);
}
To explain what this does:
array_chunkbreaks your array of years up into an array-of-arrays, each sub-array of size 5 or less.- The
foreachloop iterates over the outer array, putting each sub-array into$valin turn. - The
echostatement prints out the first element of the sub-array (returned fromreset()) followed by a dash, followed by the last element of the sub-array (returned fromend()).
Amber
2010-05-07 19:51:13
So array_chunk was it. Thank u Dav.Learned something. :)
Julious
2010-05-07 19:56:36
You also should accept the answer.
daniels
2010-05-07 19:58:56