php每分钟只获得一部分数组

时间:2017-01-02 18:59:16

标签: php arrays

如何每分钟定期获取一个数组的一部分作为每分钟10个阵列?

例如,在:

  

第一分钟:1到10
第二分钟:11到20   
三分钟:21至30阵列四分钟:31至40分钟   数组到最后完整的aryeh数是313 ....

我试着这样:

$tim = date('i');
$first = $tim + 9;
$end = $first * 2;
//This array is small:
$woides = '["2254365","2255017","2254288","2220745","2254452","2255239","2232426","2255143","2248513","2254295","2233629","2238651","2254901","2238430","2239471","2255294","2217888","2242302","2242310","2220380","56121236","2255244","2235716","2246897","2246737","2254887","2247978","2239897","2241012","2254335","2220237","2255028","2235095","2227798","2239447","2254980","2255007","2220163","2254340","2254572","2239514","2255272","2254465","2235742","2254835","2255162","2248951","2255162","2226917","2227166","2226813","2232705","2228886","2253837","2239974","2241068","2254978","2241496","2254777","2254277","2254714","2223700","2254538","2254929","2254463","2254730","2220651","2223937","2210917","2218965","2254774","2254616","2253001","2227270","2248955","2245845","2246022","2225224","2251945","56120984","2226503","2254339","2254457","2255184"]'; 
$input = json_decode($woides);
$output = array_slice($input, $first, $end);
echo '<pre>';
print_r($output);

这怎么可能更好更短?

1 个答案:

答案 0 :(得分:0)

更改$first的计算。另外array_slice需要数组的长度,而不是最后一项的索引作为第三个参数:

$tim = date('i');
$first = $tim - ($tim % 10) + 1;
$arrayLength = 10;
//This array is small:
$woides = '["2254365","2255017","2254288","2220745","2254452","2255239","2232426","2255143","2248513","2254295","2233629","2238651","2254901","2238430","2239471","2255294","2217888","2242302","2242310","2220380","56121236","2255244","2235716","2246897","2246737","2254887","2247978","2239897","2241012","2254335","2220237","2255028","2235095","2227798","2239447","2254980","2255007","2220163","2254340","2254572","2239514","2255272","2254465","2235742","2254835","2255162","2248951","2255162","2226917","2227166","2226813","2232705","2228886","2253837","2239974","2241068","2254978","2241496","2254777","2254277","2254714","2223700","2254538","2254929","2254463","2254730","2220651","2223937","2210917","2218965","2254774","2254616","2253001","2227270","2248955","2245845","2246022","2225224","2251945","56120984","2226503","2254339","2254457","2255184"]'; 
$input = json_decode($woides);
$output = array_slice($input, $first, $arrayLength); 
echo '<pre>';
print_r($output);