开发者

PHP number range menu from array

开发者 https://www.devze.com 2022-12-29 19:20 出处:网络
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

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:

  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()).
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号