note 78791 deleted from function.array by nlopess
| From: | nlopess@php.net | Date: | Sun, 28 Oct 2007 14:00:29 +0000 |
| Subject: | note 78791 deleted from function.array by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-131790@lists.php.net to get a copy of this message | ||
Note Submitter: Sam Yong - hellclanner [at] live dot com
----
I wanted to put a large range of numbers into an array but it seems to me that the array function
don't have that kind of ability for me to put in a range of numbers without having to list down
every single number.
Here's my solution:
<?php
function arr_range($r1,$r2){
// the end range is smaller than the start range
// switch around
if($r2 < $r1){
$t = $r1;
$r1 = $r2;
$r2 = $t;
unset($t);
}
$return_val = array();
$i = 0;
// for each value of the range
// add the value into the temparory array
for($i = $r1;$i<=$r2;$i++){
$return_val[] = $i;
}
// return the values in array form
return $return_val;
}
// example usages
$values = arr_range(100,6000);
$values = arr_range(1996,2010);
$values = arr_range(50,25); // it works
$values = arr_range(-64,1024);
?>