samedi 27 juin 2020

Iterating through array chunk for a function call

I have an array that has 20 objects in it (I have reduced this to 3 in this example to keep things simple).

    $dataset = array(
      array(
        "product" => $product->id,
        "unit_amount" => "2020",
        "end_date" =>  date_timestamp_get(date_create("2020-07-12")) // Payment 1 Date
    
      ),
      array(
          "product" => $product->id,
          "unit_amount" => "2000",
          "end_date" =>  date_timestamp_get(date_create("2020-07-15")) //Payment 2 Date
      ),
      array(
          "product" => $product->id,
          "unit_amount" => "3400",
          "end_date" =>  date_timestamp_get(date_create("2020-07-16")) //Payment 3 Date
      ),
)

I can split the array by using chunks:

$schedule_batch = array_chunk($dataset, 9);

Now I want to process each chunk as seperate calls

  foreach ($schedule_batch as $key => $value){
    foreach ($schedule_batch[$key] as $key2 => $value2) {
         createSchedule($customer->id, $phases);
      }
    }

However all this does it just process all of the 20 array items in one call. My goal is to allow it to run twice creating two schedules each with 10 items (not a schedule of 20 items).

Any help is really appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire