2012-11-21 209 views
0

将50 ++数字(FB页面ID)的逗号分隔列表拆分为更小的数据块并处理它们的最佳方法n一次?这份名单将是这样的:将逗号分隔的字符串分成较小的部分

$likeslist="266037406871517,81337462601,34782298000891,56859608486,8797601255,48126111909968,8807449704,3634204295,6840064056,16627954050,7581229254,282681243370,356780606553962,207549746039055,13708123117519,204852972922619,407056596017784,584207664985882,11141618921610,66707529019,271953746236343,9576298621,40575497158,29252725868524,210237443769975,469586875072133,32693104762450,262744428996,506144412803606,52385706779438"; 

因为在$ likeslist总数会有所不同(可能是数百个),是更快得到我们所用爆炸或substr_count工作的人数是多少? (问题1

$likesarray = explode(",", $likeslist); 
$result = count($likeslist); 

OR

$listtotal = substr_count($likeslist, ",") +1; 

然后,我怎样才能在每个组中划分likeslist $分成较小的组通过各ID(的比方说,5)和环? (question question 2

+1

你可以爆炸成一个列表,像你这样,并使用'for'循环每5个标识追加,并添加到数组中。无论如何,你为什么要把它分成更小的组?只需循环遍历整个列表... –

+0

我有一个喜欢的列表,并希望查询Facebook图形API,通过传递多个ID到API来找出每个喜欢的类别(例如电视节目,音乐等)一次(每次5个) – sinisterfrog

+1

您可以使用'array_chunk'将一个数组拆分为块,但是如果要以任何方式遍历所有组中的所有ID,那么该怎么办? – jeroen

回答

0

继续做一个大阵。它必须非常耗费所有的内存(并且你可以在配置中改变你的内存限制)。

要处理清单的部分,只使用一个循环和array_slice()功能:

$likeslist = explode(',', $likeslist);  
$listLen = count($likeslist); 
$chunkSize = 5; 

for($offset=0; $offset<$listLen; $offset+=$chunkSize) { 
    $subList = array_slice($likeslist, $offset, $chunkSize); 
    // do whatever to your sublist 
    print_r($subList); 
} 
0

使用多维数组。

$likes = explode(",", $likesList); 
$l5 = array(); // multi-dimensional array containing arrays of only 5 likes 
$i = 0; // counter for how many items we have per sub-array 
$c = 0; // counter for what index of $l5 we are on 
foreach($likes as $l) { 
    if ($i >= 5) { 
     $c++; // increment array index 
     $i = 0; // reset counter for the next 5 entries 
    } 
    $l5[$c][] = $l; 
    $i++; 
}