PHP:每50个项目循环功能

时间:2010-10-08 13:25:39

标签: php xml loops while-loop

情况:数组中有160个ID,需要构建xml请求,最多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);
}

7 个答案:

答案 0 :(得分:8)

您可以使用array_chunk将大数组拆分成块。

修改
这是另一个鲜为人知的数组函数(我认为在你的情况下可能需要这个函数):array_splice

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

了解你的阵列功能年轻的蚱蜢!所有 100 77。

答案 1 :(得分:3)

编辑:为此,您必须从0开始以数字方式对数组进行索引 您可以将'value to start'作为参数传递。

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);

答案 2 :(得分: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);
  }
}

答案 3 :(得分: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);
        }
}     

答案 4 :(得分: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

答案 5 :(得分: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);
}

}

答案 6 :(得分:1)

我肯定会按照Alin Purcaru的建议去array_splice() 使用array_splice,您可以:

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

通过这种方式,您获得$chunk的最大值。您可以轻松处理的50件商品。