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

这篇文章主要介绍了php根据一个给定范围和步进生成数组的方法,涉及php针对数组的遍历技巧,需要的朋友可以参考下

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

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 ) */

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

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/71206192e9ac8ee62d27fb7fb1fe534b.html