srph/map-range

更高效的 foreach-range

v0.1.0 2015-08-26 19:11 UTC

This package is not auto-updated.

Last update: 2024-09-18 09:51:38 UTC


README

composer require srph/map-range

更高效的 foreach-range.

用法

SRPH\MapRange\map_range(function($index) {
	// do something
}, $from, $to);

对于 PHP >=v5.6,你可以使用 use 函数(即导入函数)语法

use function SRPH\MapRange\map_range;

map_range(function($index) {
	// do something
}, $from, $to);

注意它是在 while $from <= $to 的过程中迭代的。

原因

因为一些开发者更喜欢这种方式

foreach(range(0, 10) as $i) {
	// ..
} 

而不是这种方式

for ( $i = 0; $i < 10; $i++ ) {
	//
}

其中第一个示例首先生成一个缓冲数组(这对性能非常不利)。