2010-10-08 59 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個項目。