(PHP 4, PHP 5)
range — 建立一个包含指定范围单元的数组
start
序列的第一个值。
limit
序列结束于 limit
的值。
step
如果给出了 step
的值,它将被作为单元之间的步进值。step
应该为正值。如果未指定,step
则默认为 1。
返回的数组中从
start
到 limit
的单元,包括它们本身。
版本 | 说明 |
---|---|
5.0.0 |
添加了可选参数 step 。
|
4.1.0 to 4.3.2 | 在 PHP 版本 4.1.0 到 4.3.2 中, range() 将数字字符串看作字符串而不是整数,因此将会被作为字符序列使用。例如,"4242" 会被当作 "4" 来对待。 |
4.1.0 | 在版本 4.1.0 之前 range() 函数只产生递增的整数数组。对于字符序列和递减数组的支持是 4.1.0 加入的。字符序列值的长度限定为一。如果输入的长度超过一,则只使用第一个字符。 |
Example #1 range() 例子
<?php
// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
foreach (range(0, 12) as $number) {
echo $number;
}
// The step parameter was introduced in 5.0.0
// array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
foreach (range(0, 100, 10) as $number) {
echo $number;
}
// Use of character sequences introduced in 4.1.0
// array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
foreach (range('a', 'i') as $letter) {
echo $letter;
}
// array('c', 'b', 'a');
foreach (range('c', 'a') as $letter) {
echo $letter;
}
?>
Ray.Paseur often uses Gmail (2013-06-06 15:16:33)
Interestingly, these two statements produce identical 26-character alphabet arrays.
<?php
$arr = range('A', 'Z');
$arr = range('AA', 'ZZ');
gtisza at gmail dot com (2013-04-25 10:27:44)
You might expect range($n, $n-1) to be an empty array (as in e.g. Python) but actually PHP will assume a step of -1 if start is larger than end.
Sysix (2013-03-01 09:58:27)
for is better than range:
<?php
$time_start = microtime(true);
foreach(range(1, 50000) as $val) {
echo $val.',';
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo '<br /><br />'.$time.'<br /> <br />';
$time_start = microtime(true);
for($i = 1; $i <= 50000; $i++) {
echo $i.',';
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo '<br /><br />'.$time.'<br /> <br />';
?>
Range: 0.41276001930237
For: 0.3721399307251
Palz (2012-10-29 22:59:15)
To create a range array like
Array
(
[11] => 1
[12] => 2
[13] => 3
[14] => 4
)
combine two range arrays using array_combine:
array_combine(range(11,14),range(1,4))
ktamas77 at gmail dot com (2012-08-03 22:59:08)
if you need zero padding, string prefixes or any other masks, then a simple combination of array_map, inline functions and sprintf is your friend.
<?php
$a = array_map(function($n) { return sprintf('sample_%03d', $n); }, range(50, 59) );
print_r($a);
?>
Will result:
Array
(
[0] => sample_050
[1] => sample_051
[2] => sample_052
[3] => sample_053
[4] => sample_054
[5] => sample_055
[6] => sample_056
[7] => sample_057
[8] => sample_058
[9] => sample_059
)
me at phpscott dot com (2012-05-23 17:37:42)
So, I needed a quick and dirty way to create a dropdown select for hours, minutes and seconds using 2 digit formatting, and to create those arrays of data, I combined range with array merge..
<?php
$prepend = array('00','01','02','03','04','05','06','07','08','09');
$hours = array_merge($prepend,range(10, 23));
$minutes = array_merge($prepend,range(10, 59));
$seconds = $minutes;
?>
Super simple.
dries at volta dot be (2012-02-09 11:30:28)
Ever wanted to generate an array with a range of column names for use in Excel file related parsing?
I've wrote a function that starts at the A column and adds column names up until the column you specified.
<?php
/**
* This function creates an array with column names up until the column
* you specified.
*/
function createColumnsArray($end_column, $first_letters = '')
{
$columns = array();
$length = strlen($end_column);
$letters = range('A', 'Z');
// Iterate over 26 letters.
foreach ($letters as $letter) {
// Paste the $first_letters before the next.
$column = $first_letters . $letter;
// Add the column to the final array.
$columns[] = $column;
// If it was the end column that was added, return the columns.
if ($column == $end_column)
return $columns;
}
// Add the column children.
foreach ($columns as $column) {
// Don't itterate if the $end_column was already set in a previous itteration.
// Stop iterating if you've reached the maximum character length.
if (!in_array($end_column, $columns) && strlen($column) < $length) {
$new_columns = createColumnsArray($end_column, $column);
// Merge the new columns which were created with the final columns array.
$columns = array_merge($columns, $new_columns);
}
}
return $columns;
}
?>
Usage:
<?php
// Return an array with all column names from A until and with BI.
createColumnsArray('BI');
?>
Aram Kocharyan (2011-02-27 19:58:47)
Here's a function to generate ranges from strings:
<?php
/* Creates an array of integers based on a given range string of format "int - int"
Eg. range_str('2 - 5'); */
function range_str($str) {
preg_match('#(\\d+)\\s*-\\s*(\\d+)#', $str, $matches);
if ( count($matches) == 3 ) {
return range($matches[1], $matches[2]);
}
return FALSE;
}
// Test
$array = range_str(' 2 - 4 ');
print_r($array);
?>
This outputs:
Array
(
[0] => 2
[1] => 3
[2] => 4
)
jay at NOspam dot myd3 dot com (2009-04-29 17:54:59)
This is a modified version of thomas' range_string() function. It's simpler, cleaner, and more robust, but it lacks the advanced features his function had, hopefully it will be of assitance to someone.
Examples:
input: "1, 2, 3, 4, 5, 6" --> output: 1, 2, 3, 4, 5, 6
input: "1-6" --> output: 1, 2, 3, 4, 5, 6
input: "1-6" --> output: 1, 2, 3, 4, 5, 6
input: "1 - -6" --> output: 1, 2, 3, 4, 5, 6
input: "0 - 0" --> output: 0
input: "1, 4-6, 2" --> output: 1, 2, 4, 5, 6
input: "6,3-1" --> output: 1, 2, 3, 6
<?php
define('RANGE_ARRAY_SORT', 1);
define('RANGE_ARRAY', 2);
define('RANGE_STRING_SORT', 3);
define('RANGE_STRING', 4);
function range_string($range_str, $output_type = RANGE_ARRAY_SORT)
{
// Remove spaces and nother non-essential characters
$find[] = "/[^\d,\-]/";
$replace[] = "";
// Remove duplicate hyphens
$find[] = "/\-+/";
$replace[] = "-";
// Remove duplicate commas
$find[] = "/\,+/";
$replace[] = ",";
$range_str = preg_replace($find, $replace, $range_str);
// Remove any commas or hypens from the end of the string
$range_str = trim($range_str,",-");
$range_out = array();
$ranges = explode(",", $range_str);
foreach($ranges as $range)
{
if(is_numeric($range) || strlen($range) == 1)
{
// Just a number; add it to the list.
$range_out[] = (int) $range;
}
else if(is_string($range))
{
// Is probably a range of values.
$range_exp = preg_split("/(\D)/",$range,-1,PREG_SPLIT_DELIM_CAPTURE);
$start = $range_exp[0];
$end = $range_exp[2];
if($start > $end)
{
for($i = $start; $i >= $end; $i -= 1)
{
$range_out[] = (int) $i;
}
}
else
{
for($i = $start; $i <= $end; $i += 1)
{
$range_out[] = (int) $i;
}
}
}
}
switch ($output_type) {
case RANGE_ARRAY_SORT:
$range_out = array_unique($range_out);
sort($range_out);
case RANGE_ARRAY:
return $range_out;
break;
case RANGE_STRING_SORT:
$range_out = array_unique($range_out);
sort($range_out);
case RANGE_STRING:
default:
return implode(", ", $range_out);
break;
}
}
// Sample Usage:
$range = range_string("6, 3-1");
?>
thomas+php1 at tgohome dot com (2008-09-30 08:44:50)
Range as a string. Items are separated with a comma; which can be in any of the following formats:
"1, 2, 3, 4, 5, 6" - output: 1, 2, 3, 4, 5, 6
"1 - 6" - output: 1, 2, 3, 4, 5, 6
"1 -%2 6" - output: 1, 3, 5 (last number will not be counted unless it evenly fits in)
"1 - -6" - output: 1, 0, -1, -2, -3, -4, -5, -6
"0 - 0" - output: 0
"1, 2, 3, [LAST_NUM] - 6" - output: 1, 2, 3, 3, 4, 5, 6 (note repeated 3)
"1, 2, 3, [LAST_NUM+1] - 6" - output: 1, 2, 3, 4, 5, 6 (no repeated 3)
"1, 2, 3, [LAST_NUM+-1] - 6" - output: 1, 2, 3, 2, 3, 4, 5, 6
<?php
define('RANGE_ARRAY', 1);
define('RANGE_STRING', 2);
function range_string($range_str, $output_type = RANGE_ARRAY)
{
$range_out = array();
$ranges = explode(",", $range_str);
$last_num = 0;
foreach($ranges as $range)
{
$step = 1;
$range = trim($range);
if(is_numeric($range))
{
// Just a number; add it to the list.
$range_out[] = $range;
$last_num = $range;
}
else if(is_string($range))
{
// Figure out if it is just a character.
if(strlen($range) == 1)
{
$range_out[] = (string)$range;
$last_num = 0;
}
else
{
// Is probably a range of values.
$range_exp = explode(" ", $range);
if(substr($range_exp[1], 0, 1) == '-' && !is_numeric(substr($range_exp[1], 0, 1)))
{
// Jumping range?
$jump = str_split($range_exp[1], 1);
if(count($jump) > 0)
{
if($jump[1] == '%')
{
$step = substr($range_exp[1], 2);
}
}
else
{
// Normal range.
$step = 1;
}
}
else
{
$step = 1;
}
if($range_exp[0] == '[LAST_NUM]')
{
$start = $last_num;
}
else
{
$exp = explode("+", $range_exp[0]);
if($exp[0] == '[LAST_NUM')
{
$start = $last_num + trim($exp[1], ']');
}
else
{
$start = $range_exp[0];
}
}
$end = $range_exp[2];
if($start > $end)
{
for($i = $start; $i >= $end; $i -= $step)
{
$range_out[] = $i;
}
$last_num = $i;
}
else
{
for($i = $start; $i <= $end; $i += $step)
{
$range_out[] = $i;
}
$last_num = $i;
}
// echo $step . ", ";
}
}
}
if($output_type == RANGE_ARRAY)
{
return $range_out;
}
else
{
return implode(", ", $range_out);
}
}
echo range_string("1, 2, 3, [LAST_NUM+1] - 6", RANGE_STRING);
?>
ThinkMedical at Gmail dot com (2008-08-26 05:11:42)
foreach(range()) whilst efficiant in other languages, such as python, it is not (compared to a for) in php*.
php is a C-inspired language and thus for is entirely in-keeping with the lanuage aethetic to use it
<?php
//efficiant
for($i = $start; $i < $end; $i+=$step)
{
//do something with array
}
//inefficiant
foreach(range($start, $end, $step) as $i)
{
//do something with array
}
?>
That the officiant documentation doesnt mention the for loop is strange.
Note however, that in PHP5 foreach is faster than for when iterating without incrementing a variable.
* My tests using microtime and 100 000 iterations consistently (~10 times) show that for is 4x faster than foreach(range()).
captvanhalen at gmail dot com (2008-03-27 10:33:10)
Here is a home rolled range() function that uses the step feature for those unfortunate souls who cannot use PHP5:
<?php
function my_range( $start, $end, $step = 1) {
$range = array();
foreach (range( $start, $end ) as $index) {
if (! (($index - $start) % $step) ) {
$range[] = $index;
}
}
return $range;
}
?>
chris at laflash dot org (2007-05-09 16:47:42)
Quick HTML menus with minimum and maximum sets of years:
<?php
/*
** Quick HTML menus with minimum and maximum sets of years.
** @author Chris Charlton <chris@laflash.org>
** @license FREE!
*/
// Years range setup
$year_built_min = 1900;
$year_built_max = date("Y");
?>
<select id="yearBuiltMin" size="1">
<?php // Generate minimum years
foreach (range($year_built_min, $year_built_max) as $year) { ?>
<option value="<?php echo($year); ?>"><?php echo($year); ?></option>
<?php } ?>
</select>
<select id="yearBuiltMax" size="1">
<?php // Generate max years
foreach (range($year_built_max, $year_built_min) as $year) { ?>
<option value="<?php echo($year); ?>"><?php echo($year); ?></option>
<?php } ?>
</select>
m0sh3 at hotmail dot com (2007-03-01 17:46:05)
Here's how i use it to check if array is associative or not:
<?php
if (array_keys($arr)===range(0, sizeof($arr)-1)) {
// not associative array
} else {
// associative array
}
?>
manuel at levante dot de (2006-11-07 05:25:15)
<?php
function srange ($s) {
preg_match_all("/([0-9]{1,2})-?([0-9]{0,2}) ?,?;?/", $s, $a);
$n = array ();
foreach ($a[1] as $k => $v) {
$n = array_merge ($n, range ($v, (empty($a[2][$k])?$v:$a[2][$k])));
}
return ($n);
}
$s = '1-4 6-7 9-10';
print_r(srange($s));
?>
Return:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 6
[5] => 7
[6] => 9
[7] => 10
)
subscription101 at hotmail dot com (2006-01-08 06:36:47)
A much simpler way of creating a range of even numbers is by starting with an even number:
<?php
range(2, 10, 2);
?>
emory underscore smith at hotmail (2005-08-20 19:53:48)
since its not stated explicitly above, thought id point out that you arent limited to using integers.
however, be careful when doing so, as you might not get the range you expect!
to illustrate:
<?php
$am = range(500,1600,10);
$fm = range(88.1,107.9,.2);
print_r($am);
print_r($fm);
?>
print_r($am) yields the expected result:
Array
(
[0] => 500
[1] => 510
[2] => 520
...
[109] => 1590
[110] => 1600
)
print_r($fm), however, falls a bit (1%) short:
Array
(
[0] => 88.1
[1] => 88.3
[2] => 88.5
...
[97] => 107.5
[98] => 107.7
)
so, if you want to use a non-integral step size params for numeric ranges, be sure to account for fp representation accuracy and error accumulation; a step size of something like pi or 1/10 could spell disaster for a large range. if in doubt, use integral steps and divide ... something like <?php range(88.1,108,.2) ?> might work to recover 107.9, but would not be scalable like, say <?php array_map(create_function('$x','return $x/10;'),range(881,1079,2)) ?>.
-emory
derek at php dot net (2005-05-08 06:13:47)
This should emulate range() a little better.
<?php
function range_wroar($low, $high, $step = 1) {
$arr = array();
$step = (abs($step)>0)?abs($step):1;
$sign = ($low<=$high)?1:-1;
if(is_numeric($low) && is_numeric($high)) {
//numeric sequence
for ($i = (float)$low; $i*$sign <= $high*$sign; $i += $step*$sign)
$arr[] = $i;
} else {
//character sequence
if (is_numeric($low))
return $this->range($low, 0, $step);
if (is_numeric($high))
return $this->range(0, $high, $step);
$low = ord($low);
$high = ord($high);
for ($i = $low; $i*$sign <= $high*$sign; $i += $step*$sign) {
$arr[] = chr($i);
}
}
return $arr;
}
?>
j dot gizmo at aon dot at (2004-09-23 04:23:43)
i figured i'd add some more functionality to the myRange() functions below.
now you can, besides giving a $step parameter,
1. count backwards
2. count with letters
3. give whatever parameter you want, there's nothing (i know of) that will cause an endless loop (try a negative $step for the previous function....)
<?php
function myRange($num1, $num2, $step=1)
{
if (is_numeric($num1) && is_numeric($num2))
{
//we have a numeric range
$step = ( abs($step)>0 ? abs($step) : 1 ); //make $step positive
$dir = ($num1<=$num2 ? 1 : -1); //get the direction
for($i = (float)$num1; $i*$dir <= $num2*$dir; $i += $step*$dir)
{
$temp[] = $i;
}
}
else
{
//we have a character range
$num1=ord((string)$num1); //convert to ascii value
$num2=ord((string)$num2);
$step = ( abs($step)>0 ? abs($step) : 1 ); //make $step positive
$dir = ($num1<=$num2 ? 1 : -1); //get direction
for($i = $num1; $i*$dir <= $num2*$dir; $i += $step*$dir)
{
$temp[] = chr($i);
}
}
return $temp;
}
print_r(myRange( 1, 3, 0.5 )); //you can use fractional steps
print_r(myRange( "a", "k", 3 )); //or count letters
print_r(myRange( "5", "9" )); //numbers are detected even if hidden in strtings
print_r(myRange( "!", "%", 1/pi() )); //or mess around with senseless parameters
?>
donwilson at gmail dot com (2004-08-31 21:38:10)
To speed your MyRange() function, I have created a much nicer function with less code to sift through to include the step parameter.
<?php
// range() limitation for PHP <5.0.0
function myRange($num1, $num2, $step=1)
{
for($i = $num1; $i <= $num2; $i += $step)
{
$temp[] = $i;
}
return $temp;
}
?>
For whatever reason my comment was deleted..?
Forrester at tfcustomized dot com (2004-05-17 19:57:16)
Since users of < PHP 5.0.0 don't have the option of the step parameter, I've created a little function to account for it:
@USAGE: (int low, int high [, int step])
function myRange($low,$high,$step=1)
{
$ranArray = range($low,$high);
$step--;
$keys = count($ranArray);
for($i=0;$i<$keys;$i++)
{
$retArray[] = $ranArray[$i];
$i = $i + $step;
}
return $retArray;
}
// Example usage:
print_r(myRange(1,11,2));
// Returns the array:
// [0] => 1
// [1] => 3
// [2] => 5
// [3] => 7
// [4] => 9
// [5] => 11