繁体   English   中英

在 Curl function 中运行 foreach 循环

[英]Running a foreach loop within a Curl function

我需要在 curl 命令中运行一个 foreach 循环,但不能完全正确地获取语法。 我已经很好地解析了其他变量,但它只是格式化我的数组以适应。

   curl_setopt($ch, CURLOPT_POSTFIELDS, "{
      \"order\": {
        \"channel_id\": $channel_id,
        \"customer_id\": $customer_id,
        \"deliver_to_id\": $deliver_to_id,
        \"delivery_method_id\": $delivery_method_id,
        \"line_items_attributes\": [
          {
            \"sellable_id\": 39236017,
            \"price_per_unit\": \"1.000\",
            \"quantity\": \"10\"
          }
        ],
      }
    }");

这是我的数组:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 39235995
                    [quantity] => 1
                    [price] => 2.81
                )

            [1] => Array
                (
                    [id] => 39235995
                    [quantity] => 1
                    [price] => 2.81
                )

            [2] => Array
                (
                    [id] => 39236029
                    [quantity] => 0
                    [price] => 2.952
                )

            [3] => Array
                (
                    [id] => 39236015
                    [quantity] => 0
                    [price] => 3.333
                )

        )

)

我需要将我的数组专门推入这个部分:

\"sellable_id\": 39236017,
\"price_per_unit\": \"1.000\",
\"quantity\": \"10\"

这是我的尝试,但这会导致各种问题,我哪里出错了?

curl_setopt($ch, CURLOPT_POSTFIELDS, "{
  \"order\": {
    \"channel_id\": $channel_id,
    \"customer_id\": $customer_id,
    \"deliver_to_id\": $deliver_to_id,
    \"delivery_method_id\": $delivery_method_id,
    \"line_items_attributes\": [
      {
        foreach($parcels[0] as $item){
          \"sellable_id\": $item['id'],
          \"price_per_unit\": $item['price'],
          \"quantity\": $item['quantity'],
        }
      }
    ],
  }
}");

稍微补充一下,如果数组是这个并且我没有使用foreach($parcels[0] as $item) {而是foreach($parcels as $item) {来定位它怎么办?

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [id] => 39235995
                    [quantity] => 1
                    [price] => 2.46
                )

            [1] => Array
                (
                    [id] => 39236017
                    [quantity] => 1
                    [price] => 2.75
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [id] => 39236029
                    [quantity] => 1
                    [price] => 2.58
                )

        )

)

您不能只将 PHP 代码放在字符串中并期望它被执行。 您必须转换您的数组,然后传递给 cUrl:

$order = [
  "order" => [
    "channel_id" => $channel_id,
    "customer_id" => $customer_id,
    "deliver_to_id" => $deliver_to_id,
    "delivery_method_id" => $delivery_method_id,
    "line_items_attributes" => [
      [
        "sellable_id": 39236017,
        "price_per_unit": "1.000",
        "quantity": "10"
      ]
    ],
  ]
];

foreach($parcels[0] as $item) {
    $order['order']['line_items_attributes'][] = [
        "sellable_id" => $item['id'],
        "price_per_unit" => $item['price'],
        "quantity" => $item['quantity'],
    ];      
}

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($order));

暂无
暂无

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

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