开发者

PHP: generate a number from xyz0000 to xyz9999

开发者 https://www.devze.com 2023-02-09 18:32 出处:网络
I am trying to generate a list of numbers (that I can use in a for/foreach cycle). Any number should be 10 digits long and have an initial prefix (i.e. 0851)

I am trying to generate a list of numbers (that I can use in a for/foreach cycle).

Any number should be 10 digits long and have an initial prefix (i.e. 0851)

0851xxxxxx

and开发者_如何转开发 go from 0851000000

to

0851999999


range('0851000000','0851999999')

However this array will take as much as 84MB in memory (as reported by memory_get_usage(1))

Less memory consuming way is to generate these numbers on the fly, while you iterate in your loop.

For example

for($a = 851000000; $a <= 851999999; $a++) {
  $number = '0'.(string)$a;
  doSomethingWith($number);
}


For a random generation, use the following code. Use Mchl's answer if you want the entire list of possibilities.

$str = sprintf("0851%06d", rand(0, 999999));
0

精彩评论

暂无评论...
验证码 换一张
取 消