tags:

views:

32

answers:

1

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:

  1. array_chunk breaks your array of years up into an array-of-arrays, each sub-array of size 5 or less.
  2. The foreach loop iterates over the outer array, putting each sub-array into $val in turn.
  3. The echo statement prints out the first element of the sub-array (returned from reset()) followed by a dash, followed by the last element of the sub-array (returned from end()).
Amber
So array_chunk was it. Thank u Dav.Learned something. :)
Julious
You also should accept the answer.
daniels