使用array_chunk在php中切割数组

前一个例子中,如果1w3k条记录一起用curl来迸发,会导致一个问题,那就是大量的超时,因为系统迸发连接数有限,我测试了一下,吧url数组切割为200个刚好差不多,一开始我还傻乎乎的用do while来切割,原来php内置了这个函数
see:http://php.net/manual/en/function.array-chunk.php


[php]
$urls=Array(
'0'=>'http://cpc.people.com.cn/GB/64114/75347/',
'1'=>'http://www.moe.edu.cn',
'2'=>'http://www.mof.gov.cn',
3=>'http://www.mohrss.gov.cn',
4=>'http://www.jszzb.gov.cn',
5=>'http://www.ec.js.edu.cn');
$urls=array_chunk($urls, 2, true);
print_r($urls);
[/php]
输出
[php]Array
(
[0] => Array
(
[0] => http://cpc.people.com.cn/GB/64114/75347/
[1] => http://www.moe.edu.cn
)

[1] => Array
(
[2] => http://www.mof.gov.cn
[3] => http://www.mohrss.gov.cn
)

[2] => Array
(
[4] => http://www.jszzb.gov.cn
[5] => http://www.ec.js.edu.cn
)

)
[/php]


[php]$urls=array_chunk($urls, 2, false);[/php]
输出:
[php]Array
(
[0] => Array
(
[0] => http://cpc.people.com.cn/GB/64114/75347/
[1] => http://www.moe.edu.cn
)

[1] => Array
(
[0] => http://www.mof.gov.cn
[1] => http://www.mohrss.gov.cn
)

[2] => Array
(
[0] => http://www.jszzb.gov.cn
[1] => http://www.ec.js.edu.cn
)

)
[/php]
使用true会保留原始数组中的键名,这个就是我需要的。默认是FALSE。

Author Info :
  • From:使用array_chunk在php中切割数组
  • URL:https://blog.ihipop.com/2010/07/1331.html
  • Please Reserve This Link,Thanks!
  • 发表回复

    您的电子邮箱地址不会被公开。 必填项已用*标注