tags:

views:

102

answers:

7

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);
}
+3  A: 

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

Adam
+7  A: 

Hi,

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 of them.

Alin Purcaru
+1 for array chunk, would work with associated/indexed arrays and not dependant on the index being numerical.
Ross
agreed, +1 for array_chunk
Raoul Duke
Array_chunk seems to solve it! Thanks a lot :)
MeProtozoan
+1  A: 

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);
  }
}
Raoul Duke
+1  A: 

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);
        }
}     
codaddict
+1  A: 

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

Felix Kling
+1  A: 

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

}

kizzie33
+1  A: 

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.

Dennis Haarbrink