百科狗-知识改变命运!
--

range() - 根据范围创建数组,包含指定的元素 - php 数组函数

是丫丫呀1年前 (2023-11-21)阅读数 24#技术干货
文章标签数组

range()

(PHP 4, PHP 5, PHP 7)

根据范围创建数组,包含指定的元素

说明

range(mixed $start,mixed $end[,number$step= 1]): array

建立一个包含指定范围单元的数组。

参数

$start

序列的第一个值。

$end

序列结束于$end的值。

$step

如果设置了步长$step,会被作为单元之间的步进值。$step应该为正值。不设置$step则默认为 1。

返回值

返回的数组中从$start到$end(含 start 和 end)的单元。

范例

range() - 根据范围创建数组,包含指定的元素 - php 数组函数

Example #1range()例子

注释

Note:

字符序列值仅限单个字符。如果长度大于1,仅仅使用第一个字符。

参见

  • shuffle() 打乱数组
  • array_fill() 用给定的值填充数组
  • foreach
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))
So with the introduction of single-character ranges to the range() function, the internal function tries to be "smart", and (I am inferring from behavior here) apparently checks the type of the incoming values. If one is numeric, including numeric string, then the other is treated as numeric; if it is a non-numeric string, it is treated as zero. 
But.
If you pass in a numeric string in such a way that is is forced to be recognized as type string and not type numeric, range() will function quite differently.
Compare:
=
I wouldn't call this a bug, because IMO it is even more useful than the stock usage of the function.
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

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()).
I needed a function, that creates a letter range with arbitrary length.
You specify via the $length parameter, how many entries you need.
Logic is analog to the logic of the column-titles in a calc-sheet. 
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..

Super simple.
The function will generate an array of integers even if your numerical parameters are enclosed in quotes.

An easy way to get an array of strings is to map strval() to the range: 
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.

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
)
The function "range" is very useful to get an array of characters as range('C','R') does.
At work, I had to extend the function range($a,$b) to work in this special case: with two uppercase strings $a and $b, it should return all the possible strings between $a and $b.
This could be used for example to get the excel column indexes.
e.g. 
So I wrote the function getrange($min,$max) that exactly does this. 
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.
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 
Quick HTML menus with minimum and maximum sets of years:


  
    
Usage: 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)