【问题标题】:PHP foreach syntax in object [duplicate]对象中的PHP foreach语法[重复]
【发布时间】:2020-07-22 09:24:36
【问题描述】:

我在这样的对象中尝试 foreach:

                            $data = '{
                                      "purchase_country": "'.$this->session->data['payment_address']['iso_code_2'].'",
                                              "purchase_currency": "'.$this->session->data['currency'].'",
                                              "locale": "'.$this->session->data['language'].'",
                                              "order_amount": '.$total.',
                                              "order_tax_amount":                       0,

                                              "order_lines": [{

                                                    '. foreach ($this->cart->getProducts() as $product) { .'
                                                    "reference": "19-402",
                                                    "name": "'.$product['name'].'",
                                                    "quantity": 1,
                                                    "unit_price": 210,
                                                    "tax_rate": 0,
                                                    "total_amount": 210,
                                                    "total_tax_amount": 0,
                                                    "image_url": "https://www.exampleobjects.com/logo.png",
                                                    "product_url": "https://www.estore.com/products/f2a8d7e34",
                                                '.}.'

                                              }]
                                             }';

但似乎语法不正确。我收到了 Parse 错误:语法错误,意外的 'foreach' (T_FOREACH)

对象中是否可以使用 foreach?

【问题讨论】:

  • 您不能在变量中执行 foreach。您正在尝试将字符串与 foreach 连接
  • 好的,但是我怎样才能用数据填充 order_lines 呢?你有例子吗?
  • 构建一个多维数组并使用json_encode,这比像这样连接字符串要容易得多。

标签: php object foreach


【解决方案1】:

这应该可以解决问题,因为此时您不能使用 foreach 循环。

$data = '{
                                      "purchase_country": "'.$this->session->data['payment_address']['iso_code_2'].'",
                                              "purchase_currency": "'.$this->session->data['currency'].'",
                                              "locale": "'.$this->session->data['language'].'",
                                              "order_amount": '.$total.',
                                              "order_tax_amount":                       0,

                                              "order_lines": [{

                                                    ';
foreach ($this->cart->getProducts() as $product) { 
    $data = $data . '"reference": "19-402",
                                                    "name": "'.$product['name'].'",
                                                    "quantity": 1,
                                                    "unit_price": 210,
                                                    "tax_rate": 0,
                                                    "total_amount": 210,
                                                    "total_tax_amount": 0,
                                                    "image_url": "https://www.exampleobjects.com/logo.png",
                                                    "product_url": "https://www.estore.com/products/f2a8d7e34",
                                                ';
}
$data=$data.'                                 }]
                                             }';

【讨论】:

  • 完美运行,谢谢!
【解决方案2】:

我在 Javascript 中对 order_lines 进行了同样的尝试,但我的语法有问题。你有什么提示吗?

$('#button-confirm').on('click', function() {

    var cart_products = <?php echo json_encode($cart_products); ?>;
    data_concat = '';
    

    Klarna.Payments.authorize(
        {
          "purchase_country": "DE",
          "order_tax_amount": 0,

          "order_lines":
          [';

                    $(cart_products).each(function(key, value_data){

                        data_concat = data_concat += '{';
                                                    data_concat += '"name":'+value_data.name+',' ';
                                                    data_concat += '"quantity":'+1+',' ',
                                                    data_concat += '}';

                    });

          data_concat = data_concat + '],

          "customer":
          {
            "date_of_birth": "1970-01-01",
          }
        }, function(res) {

            execute_ajax();
    })

});

【讨论】:

    猜你喜欢
    • 2011-10-01
    • 2014-01-31
    • 1970-01-01
    • 2013-11-09
    • 2014-05-15
    • 2014-08-07
    • 2015-02-02
    • 2016-04-20
    • 1970-01-01
    相关资源
    最近更新 更多