Home > Backend Development > PHP Tutorial > php根据一个给定范围和步进生成数组的方法_php技巧

php根据一个给定范围和步进生成数组的方法_php技巧

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 20:13:15
Original
1006 people have browsed it

这里给定开始和结束值,再给定一个步进值,就可以生成一个等差数组。

function array_range($from, $to, $step=1){
  $array = array();
  for ($x=$from; $x <= $to; $x += $step){
    $array[] = $x;
  }
  return $array;
}
print_r(array_range(0, 20, 5));
/*
returns:
Array
(
  [0] => 0
  [1] => 5
  [2] => 10
  [3] => 15
  [4] => 20
)
*/

Copy after login

希望本文所述对大家的php程序设计有所帮助。

Related labels:
php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template