简体   繁体   English

数组中的PHP数字范围菜单

[英]PHP number range menu from array

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. 我有一个像这样的Array(2010,2009,2008 ... 1992)的php数组,我想创建一个循环来打印一个菜单,菜单范围为倒数四年,例如2010-2006 2005-2001 2000-1996等。我该怎么做>我尝试的所有事情都陷入了一个无休止的循环。 THnx in advance. 提前THnx。 J. 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. array_chunk将您的年份数组分解为一个数组,每个子数组的大小为5或更小。
  2. The foreach loop iterates over the outer array, putting each sub-array into $val in turn. foreach循环遍历外部数组,依次将每个子数组放入$val中。
  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() ). echo语句打印出子数组的第一个元素(从reset()返回),后跟一个破折号,然后是子数组的最后一个元素(从end()返回)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM