【问题标题】:How to generate multi-level associative array in a loop如何在循环中生成多级关联数组
【发布时间】:2021-01-29 10:50:21
【问题描述】:

我需要传入一系列行项目以使用 Stripe 生成发票。最终结果应如下所示:注意 line_items 部分。

$checkout_session = \Stripe\Checkout\Session::create([
  'payment_method_types' => ['card'],
  'line_items' => [['quantity' => 1,
                    'price_data' => ['currency' => 'CAD',
                                     'unit_amount' => 750,
                                     'product_data' => ['name' => 'Name goes here',
                                                        'description' => 'Description goes here']]],
                    ['quantity' => 1,
                     'price_data' => ['currency' => 'CAD',
                                      'unit_amount' => 450,
                                      'product_data' => ['name' => 'Name goes here',
                                                         'description' => 'Description goes here']]]
                  ],
  'mode' => 'payment',
  'success_url' => $YOUR_DOMAIN.'/success.htm',
  'cancel_url' => $YOUR_DOMAIN.'/cancel.htm',
]);

在此示例中,有两个订单项,但可以有任意数量的项目。问题当然是我需要在传递它之前生成这个订单项数组,并且这些订单项是从数据库中出来的。

因此,理想情况下,我可以在变量中生成 line_items 数组,然后直接传入该变量;像这样:

$checkout_session = \Stripe\Checkout\Session::create([
  'payment_method_types' => ['card'],
  'line_items' => [$lineitems],
  'mode' => 'payment',
  'success_url' => $YOUR_DOMAIN.'/success.htm',
  'cancel_url' => $YOUR_DOMAIN.'/cancel.htm',
]);

然而,到目前为止,还没有运气。我可以像这样在循环中生成一个字符串:

foreach($rows as $row){
   $linedescription = $row["itemname"];
   $lineamount = $row["amount"];
   $linecomment = $row["comment"];

   $lineamount = $lineamount * 100;

   if($index > 0){
     $lineitems .= ",";
   }

   $lineitems = "['quantity' => 1,'price_data' => ['currency' => '".$currencycode."','unit_amount' => ".$lineamount.", 'product_data' => ['name' => '".$linecomment."','description' => '".$linedescription."']]]";

   $index ++;
}

这正好给了我这个:

"[['quantity' => 1,
                    'price_data' => ['currency' => 'CAD',
                                     'unit_amount' => 750,
                                     'product_data' => ['name' => 'Name goes here',
                                                        'description' => 'Description goes here']]],
                    ['quantity' => 1,
                     'price_data' => ['currency' => 'CAD',
                                      'unit_amount' => 450,
                                      'product_data' => ['name' => 'Name goes here',
                                                         'description' => 'Description goes here']]]
              ]"

问题是,它是一个字符串,我需要它是一个数组。我已经尝试过 json_decode 并且我已经尝试在循环中生成一个数组而不是一个字符串,但我似乎无法得到我需要的东西。肯定有一种简单的方法可以做到这一点?

【问题讨论】:

    标签: php arrays stripe-payments


    【解决方案1】:

    您可以像构建字符串一样构建数组。为了便于阅读,我已将每个维度拆分为单独的代码行,但您可以根据需要将它们合并为一行:

    $lineitems = array();
    foreach ($rows as $row) {
        $product_data = array('name' => $row["comment"],
                              'description' => $row["itemname"]
                              );
        $price_data = array('currency' => $currencycode,
                            'unit_amount' => $row["amount"],
                            'product_data' => $product_data
                            );
        $lineitem = array('quantity' => 1,
                          'price_data' => $price_data
                          );
        $lineitems[] = $lineitem;
    }
    

    那么你可以简单地使用:

    'line_items' => $lineitems,
    

    请致电\Stripe\Checkout\Session::create

    请注意,如果没有用于测试的示例数据,很难确定,但此代码应该可以按预期工作。另请注意,从名称看来,name 条目应该是 $row['itemname']description 应该是 $row['comment']

    【讨论】:

    • 谢谢。这确实按预期生成了数组,不幸的是我的条带调用仍然不接受它。 :/ 我尝试在 product_data 等中添加单引号,但没有奏效,我尝试将 $lineitems 包装在 json_encode 中,但这也没有奏效。我注意到当我回显 json_encode 版本时,格式包括波浪括号而不是方括号。我想知道是不是这个问题。
    • 您在问题开头显示的呼叫是否有效?因为只要你使用'line_items' => $lineitems,,而不是'line_items' => [$lineitems],,这段代码应该会产生完全相同的输入数据
    • 是的,一开始的调用确实有效。我尝试传入您的示例生成的数组,就像您建议仅使用'line_items' => $lineitems 一样,但这不起作用。加载页面时出现 500 错误。 (顺便问一下,如何在评论中突出显示代码?)
    • 没关系。原来,我是个白痴。我可以发誓它没有早点工作。 ://
    • @Vincent 要在注释中突出显示为代码,请在反引号之间插入。见stackoverflow.com/editing-help#comment-formatting
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 2019-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多