2010-10-08 62 views
2

情况:在数组中有160个ID,需要建立xml请求,最多50个集合并分别提交每个集合。PHP:每50个项目的循环函数

问题:如何循环功能,并与ID 51功能doBatch($ IDS)

简化的代码继续:

function doBatch($ids) 
{ 
    $start = "<feed>"; 

    foreach($ids as $id) 
    { 
     $add .= '<entry>'$id.'</entry>'; 
    } 

    $stop = "</feed>"; 
    $data = $start.$add.$stop; 
    post($data); 
} 
+0

是数字索引的$ ids数组? – stillstanding 2010-10-08 13:31:25

+0

您是否问如何在应用程序实例之间保持状态,以便每次运行脚本时都会批量处理下一组50个ID?或者这个程序只运行一次,并且简单地多次调用'doBatch'? – meagar 2010-10-08 13:43:11

+0

ID是Youtube视频ID(如Ez_XAQKUiL8)。 该脚本将运行一次,并将多次调用doBatch – MeProtozoan 2010-10-08 13:46:16

回答

8

可以在块分割你的大阵,与array_chunk

编辑:
这里还有另一个鲜为人知阵列功能(我认为这人们可以在你的情况需要):array_splice

$arrayToLoop = array_splice($fullArray, 0, 50); 
foreach($arrayToLoop as $id){ 
} 
functionCall($fullArray); 

知道你的数组函数年轻蚱蜢!全部其中77个。

+0

对于数组块,可以与关联/索引数组一起使用,而不依赖于数字索引。 – Ross 2010-10-08 13:33:56

+1

同意,array_chunk为+1 – 2010-10-08 13:36:06

+0

Array_chunk似乎解决了它!非常感谢 :) – MeProtozoan 2010-10-08 13:43:45

3

编辑:这个工作你数组必须从0 开始数字索引您可以传入'开始值'作为参数。

function doBatch($ids, $start = 0) 
{ 
    $start = "<feed>"; 

    $end = min(count($ids), $start + 50); 

    for($i = $start; $i < $end, $i++) 
    { 
     $add .= '<entry>'.$ids[$i].'</entry>'; 
    } 

    $stop = "</feed>"; 
    $data = $start.$add.$stop; 
    post($data); 
} 

要发布10-59,叫doBatch($ids, 10);

1

我不明白你怎么想这个结构,但看看下面的代码:

function doBatch($ids) { 
    $batches = array_chunk($ids, 50); 
    foreach ($batches as $batch) { 
    $data = "<feed>"; 
    foreach ($batch as $id) { 
     $data .= "<entry>$id</entry>"; 
    } 
    $data .= "</feed>"; 
    post($data); 
    } 
} 
1

你如果要处理的一个函数调用的所有ID中,你可以做一个空间有效的方式:

function doBatch($ids,$limit) { 

     $start = "<feed>"; 
     $stop = "</feed>"; 

     $add = ''; # initialize $add 
     $i = 0;  # and i. 

     foreach($ids as $id) { 
       $add .= '<entry>'$id.'</entry>'; 
       $i++; 

       # if limit has reached..post data. 
       if($i == $limit) { 
         $i = 0;        
         post($start.$add.$stop); 
         $add = ''; 
       }  
     } 

     # post any remaining ones. 
     if($i) { 
       post($start.$add.$stop); 
     } 
}  
1

您可以利用这个事实,即PHP维护一个数组的内部指针。下面是使用each()while循环的例子:

function processIDs(&$ids, $number) { 
    reset($ids); 
    $i = 0; 
    $l = count($ids); 
    while($i <= $l) { 
     doBatch($ids, $number); 
     $i += $number; 
    } 
} 

function doBatch(&$ids, $number) { 
    $i = 0; 
    $start = "<feed>"; 
    while($i < $number && (list($key, $id) = each($ids))) { 
     $add .= '<entry>'.$id.'</entry>'; 
     $i++; 
    } 

    $stop = "</feed>"; 
    $data = $start.$add.$stop; 
    post($data); 
} 

,你将与使用:

processIDs($ids, 50); 

需要阵列中的预处理和它的作品无论是按键。当然你也可以只创建一个函数,但我只是想重用你的代码。

在这里看到一个例子:http://codepad.org/Cm3xRq8B

1

我会做到这一点。语法应该是正确的,但我一直在玩python很多,所以你可能需要纠正位。无论如何,这应该计算多少个ID。循环,做50,同时计数和删除每个循环的总数量1,将它们关闭,继续循环,直到我们用完id。

这是否是最好的方式来做到这一点我不知道,但它会工作。它的简单......是啊!

function doBatch($ids){ 
$amount = count($ids); 
$start = "<feed>"; 

while $amount > 0 
{ 
$count = 0; 
while !($count = 50 || $amount = 0) 
    { 
     $count++; 
     $amount--; 
     $add .= '<entry>'.pop($ids).'</entry>'; 
    } 

$stop = "</feed>"; 
$data = $start.$add.$stop; 
post($data); 
} 

}

1

我肯定会跟array_splice()去了阿林Purcaru的建议。
随着array_splice你可以这样做:

while($chunk = array_splice($array, 0, 50)) { 
    // do your stuff 
} 

这样,你得到$chunk“最大的秒。您可以轻松处理50个项目。