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.
foreach(array_chunk($years, 5) as $val) {
echo reset($val) . "-" . end($val);
}
To explain what this does:
array_chunk
breaks your array of years up into an array-of-arrays, each sub-array of size 5 or less.- The
foreach
loop iterates over the outer array, putting each sub-array into$val
in turn. - The
echo
statement 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()
).
精彩评论