PHP:每 50 项循环函数

发布于 2024-09-26 20:00:45 字数 426 浏览 1 评论 0原文

情况:数组中有 160 个 id,需要构建 xml 请求,最多 50 个集合,并单独提交每个集合。

问题:如何循环函数并以 ID 51 继续?function doBatch($ids)

简化代码:

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

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

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

Situation: Got 160 ids in array, need to build xml requests, in sets of max 50 and submit each set separately.

Problem: How to loop the function and continue with ID 51?function doBatch($ids)

Simplified Code:

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无可置疑 2024-10-03 20:00:45

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

编辑:
这是另一个鲜为人知的数组函数(我认为您的情况可能需要这个函数): array_splice

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

了解你的数组函数,年轻的蚱蜢!总共 100 77 个。

You can split your big array in chunks, with array_chunk.

Edit:
And here's another little known array function (and I think this one may be needed in your case): array_splice.

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

Know your array functions young grasshopper! All 100 77 of them.

夜深人未静 2024-10-03 20:00:45

编辑:为此,您的数组必须从 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);

Edit: for this to work you array must be indexed numerically from 0
You can pass in the 'value to start' as parameter.

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

To post 10-59, call doBatch($ids, 10);

好倦 2024-10-03 20:00:45

我不明白你想如何构建它,但看看下面的代码:

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

i don't understand how you want to structure this but have a look at the following code:

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);
  }
}
还给你自由 2024-10-03 20:00:45

如果您想以一种节省空间的方式处理一个函数调用中的所有 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);
        }
}     

You if want to process all ids in one function call in a space efficient way you can do:

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);
        }
}     
书间行客 2024-10-03 20:00:45

您可以利用 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

You can make use of the fact, that PHP maintains an internal pointer of an array. Here is an example which uses each() and while loops:

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

Which you would use with:

  processIDs($ids, 50);

No preprocessing of the array needed and it works regardless of the keys. Of course you can also create only one function but I just wanted to reuse your code.

See an example here: http://codepad.org/Cm3xRq8B

陌上芳菲 2024-10-03 20:00:45

我会这样做。语法应该是正确的,但我经常使用 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);
}

}

I would do this. The syntax should be right but ive been playing with python alot so you may need to correct bits. Anyway this should count how many id's. Loop round and do 50, while counting up and removing 1 of the total amount each loop, post them off, keep looping until we run out of id's.

Whether this is the best way to do it i dont know but it would work. And its simple...ish !

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

}

二智少女 2024-10-03 20:00:45

我肯定会按照 Alin Purcaru 的建议使用 array_splice()
使用 array_splice 你可以这样做:

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

这样你就可以获得 $chunk 的最大数量。 50 个项目,您可以轻松处理。

I would definitely go with array_splice() as suggested by Alin Purcaru.
With array_splice you can do:

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

This way you get $chunk's of max. 50 items which you can process easily.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文