PHP
downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

reset> <prev
Last updated: Fri, 26 Dec 2008

view this page in

range

(PHP 4, PHP 5)

rangeCrea una matriz que contiene un rango de elementos

Descripción

array range ( mixed $bajo , mixed $alto [, number $paso ] )

Crea una matriz que contiene un rango de elementos.

Lista de parámetros

bajo

Valor bajo.

alto

Valor alto.

paso

Si un valor paso es dado, éste será usado como el incremento entre elementos en la secuencia. paso debe ser definido como un número positivo. Si no se especifica, paso tendrá un valor predeterminado de 1.

Valores retornados

Devuelve una matriz de elementos desde bajo hasta alto , ambos inclusive. Si bajo > alto, la secuencia será del mayor al menor.

Registro de cambios

Versión Descripción
5.0.0 Se agregó el parámetro opcional paso .
4.1.0 a 4.3.2 En versiones de PHP desde 4.1.0 hasta 4.3.2, range() considera las cadenas numéricas como cadenas y no enteros. En su lugar, ellas serán usadas para secuencias de caracteres. Por ejemplo, "4242" es tratado como "4".
4.1.0 Antes de PHP 4.1.0, range() sólo generaba matrices de enteros incrementales. El soporte para secuencias de caracteres y matrices en decremento fue añadido en 4.1.0. Los valores de secuencia de caracteres esán limitados a una longitud de uno. Si una longitud superior a uno es ingresada, solo se usa el primer caracter.

Ejemplos

Example #1 Ejemplos de range()

<?php
// array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
foreach (range(012) as $numero) {
    echo 
$numero;
}

// El parámetro paso fue introducido en 5.0.0
// array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
foreach (range(010010) as $numero) {
    echo 
$numero;
}

// Uso de secuencias de caracteres introducidas en 4.1.0
// array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');
foreach (range('a''i') as $letra) {
    echo 
$letra;
}
// array('c', 'b', 'a');
foreach (range('c''a') as $letra) {
    echo 
$letra;
}
?>



reset> <prev
Last updated: Fri, 26 Dec 2008
 
add a note add a note User Contributed Notes
range
thomas+php1 at tgohome dot com
30-Sep-2008 03:44
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
26-Aug-2008 12:11
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
27-Mar-2008 06:33
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
09-May-2007 11:47
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
02-Mar-2007 01:46
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
07-Nov-2006 01:25
<?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
08-Jan-2006 02:36
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
21-Aug-2005 02:53
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
08-May-2005 01:13
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
23-Sep-2004 11:23
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
01-Sep-2004 04:38
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
18-May-2004 02:57
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

reset> <prev
Last updated: Fri, 26 Dec 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites