|
range
Create an array containing a range of elements
(PHP 4, PHP 5)
range() returns an array of elements from low to high, inclusive. If low > high, the sequence will be from high to low.
New parameter:
The optional step parameter was added in 5.0.0. If a step value is given, it will be used as the increment between elements in the sequence. step should be given as a positive number. If not specified, step will default to 1. Example 319. range() examples<?php
Note:
Prior to PHP 4.1.0, range() only generated incrementing integer arrays. Support for character sequences and decrementing arrays was added in 4.1.0. Character sequence values are limited to a length of one. If a length greater than one is entered, only the first character is used.
Caution:
In PHP versions 4.1.0 through 4.3.2, range() sees
numeric strings as strings and not integers. Instead, they will be
used for character sequences. For example, See also shuffle(), array_fill(), and foreach. Related Examples ( Source code ) » range Examples ( Source code ) » Quantifier Syntaxes Examples ( Source code ) » max_execution_time Examples ( Source code ) » Using popen() to Pass Data to the column Application Examples ( Source code ) » current Examples ( Source code ) » end Set the internal pointer of an array to its last element Examples ( Source code ) » next Advance the internal array pointer of an array Examples ( Source code ) » range Examples ( Source code ) » range(0,20,2) Examples ( Source code ) » range Create an array containing a range of elements Examples ( Source code ) » Port scan Examples ( Source code ) » Download big file using HttpRequest Examples ( Source code ) » Simple examples HttpMessage Code Examples / Notes » rangedonwilson
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..? derek
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; } ?> forrester
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 emory underscore smith
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 chris
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> j dot gizmo
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 ?> m0sh3
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 } ?> subscription101
A much simpler way of creating a range of even numbers is by starting with an even number: <?php range(2, 10, 2); ?> manuel
<?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 ) phpbug
<?php foreach (range('A', 'z') as $letter) { echo $letter; } ?> yields: ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz |
Change Languagearray_change_key_case array_chunk array_combine array_count_values array_diff_assoc array_diff_key array_diff_uassoc array_diff_ukey array_diff array_fill_keys array_fill array_filter array_flip array_intersect_assoc array_intersect_key array_intersect_uassoc array_intersect_ukey array_intersect array_key_exists array_keys array_map array_merge_recursive array_merge array_multisort array_pad array_pop array_product array_push array_rand array_reduce array_reverse array_search array_shift array_slice array_splice array_sum array_udiff_assoc array_udiff_uassoc array_udiff array_uintersect_assoc array_uintersect_uassoc array_uintersect array_unique array_unshift array_values array_walk_recursive array_walk array arsort asort compact count current each end extract in_array key krsort ksort list natcasesort natsort next pos prev range reset rsort shuffle sizeof sort uasort uksort usort |