简体   繁体   English

如何在循环中生成多级关联数组

[英]How to generate multi-level associative array in a loop

I need to pass in an array of line items to generate an invoice using Stripe.我需要传入一系列行项目以使用 Stripe 生成发票。 The end result should look something like this: Notice the line_items part.最终结果应如下所示: 注意 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',
]);

In this example there are two line items, but there could be any number of items.在此示例中,有两个行项目,但可以有任意数量的项目。 The issue is of course that I need to generate this array of line items before passing it in and these line items are coming out of the DB.问题当然是我需要在传入之前生成这个行项目数组,这些行项目是从数据库中出来的。

So ideally, I could generate that line_items array in a variable and just pass in that variable instead;所以理想情况下,我可以在一个变量中生成那个 line_items 数组,然后直接传入那个变量; like so:像这样:

$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',
]);

So far however, no luck.然而,到目前为止,没有运气。 I can generate a string in a loop like so:我可以像这样在循环中生成一个字符串:

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

Which gives me exactly this:这给了我这个:

"[['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']]]
              ]"

Problem is, it's a string and I need it to be an array.问题是,它是一个字符串,我需要它是一个数组。 I've tried json_decode and I've tried generating an array in the loop instead of a string but I can't seem to end up with what I need.我试过 json_decode 并且我试过在循环中生成一个数组而不是一个字符串,但我似乎无法得到我需要的结果。 Surely there's an easy way to do this?肯定有一个简单的方法来做到这一点?

You can build an array in the same way as you are building your string.您可以按照与构建字符串相同的方式构建数组。 I've split each dimension into a separate line of code for readability but you could combine them into one line if you desired:为了便于阅读,我已将每个维度拆分为单独的一行代码,但如果需要,您可以将它们合并为一行:

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

Then you can simply use:然后你可以简单地使用:

'line_items' => $lineitems,

in your call to \\Stripe\\Checkout\\Session::create .在您对\\Stripe\\Checkout\\Session::create调用中。

Note that without sample data for testing it's hard to be certain, but this code should work as expected.请注意,如果没有用于测试的样本数据,很难确定,但此代码应该按预期工作。 Also note that it would seem from the names that perhaps the name entry should be $row['itemname'] and the description should be $row['comment'] ?另请注意,从名称看来, name条目应该是$row['itemname']description应该是$row['comment']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM