PHP数组未遍历块的第一部分

时间:2020-06-28 01:36:43

标签: php arrays laravel multidimensional-array stripe-payments

我有一个名为$dataset的PHP数组,该数组已分为两个块。我正在尝试根据每个块创建两个Stripe订阅计划。

下面的代码创建两个调度,但仅针对两次创建的块schedule_batch[1]的第二部分。第一个块中的前9个项根本没有创建。

第一个块被完全忽略,第二个块被处理两次。

为了使下面的代码示例简单,我删除了数组的全部内容,但采用下面的格式,数组中有20个项目。

    $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
      ),
      array(
          "product" => $product->id,
          "unit_amount" => "3700",
          "end_date" =>  date_timestamp_get(date_create("2020-07-18")) //Payment 4 Date
      ),
    );
    
    $schedule_batch = array_chunk($dataset, 9);
    $numberofbatches = count($schedule_batch);
    
    $phases = [];
    foreach ($schedule_batch as $index => $data) {
        $phases[] = [
          'end_date' => $data[$index]["end_date"],
          'transfer_data' => [
            'amount_percent' => $fee_percentage,
            'destination' => $account],        
          'proration_behavior' => 'none',
          'plans' => [
              [
                'price_data' => [
                  'unit_amount' => $data[$index]["unit_amount"],
                  'currency' => 'usd',
                  'product' => $data[$index]["product"],
                  'recurring' => [
                      'interval' => 'month',
                  ],
                ],
                
              ],
            ],
        ];
      }
    

function createSchedule ($customer, $phases) {
  $schedule = \Stripe\SubscriptionSchedule::create([
      'customer' => $customer,
      'expand' => ['subscription'],
      'start_date' => date_timestamp_get(date_create("2020-07-10")),
      'end_behavior' => 'cancel',
      'phases' => $phases,
    ]);
  }
    
    foreach ($schedule_batch as $index => $data) {
      createSchedule($customer->id, $phases);
    }

1 个答案:

答案 0 :(得分:1)

将生成$phases的代码应用于$dataset,然后将其分块到$schedule_batch会更容易:

$phases = [];
foreach ($dataset as $data) {
    $phases[] = [
      'end_date' => $data["end_date"],
      'transfer_data' => [
        'amount_percent' => 20,
        'destination' => 10],        
      'proration_behavior' => 'none',
      'plans' => [
          [
            'price_data' => [
              'unit_amount' => $data["unit_amount"],
              'currency' => 'usd',
              'product' => $data["product"],
              'recurring' => [
                  'interval' => 'month',
              ],
            ],
            
          ],
        ],
    ];
  }

$schedule_batch = array_chunk($phases, 2);
$numberofbatches = count($schedule_batch);

Demo on 3v4l.org

您要调用的createSchedule的最终代码为:

foreach ($schedule_batch as $phase) {
    createSchedule($customer->id, $phase);
}