简体   繁体   English

PHP在数组中组合数组

[英]PHP Combining Array within array

I'm writing an API and to send data to the application I need some arrays formatted in a certain way I can't seem to get right.我正在编写一个 API 并将数据发送到应用程序,我需要一些以某种方式格式化的数组,我似乎无法正确处理。

The message exists out of two arrays: Order and Items.该消息存在于两个数组之外:Order 和 Items。 Order is a single array, Items seems to be an array inside an array which goes wrong when multiple items are ordered. Order 是单个数组,Items 似乎是数组内的一个数组,当订购多个项目时会出错。

Required format:所需格式:

    {
  "order": {
    "email": "string",
    "phone": "string",
    "company": "string",
    "firstName": "string",
    ...
  },
  "items": [
    {
      "description": "string",
      "orderLineId": 0,
      "priceType": "string",
      "productCode": "string",
      "productDescription": "string",
      "quantity": 0,
      "unitPrice": 0
    }
    {
      "description": "string",
      "orderLineId": 0,
      "priceType": "string",
      "productCode": "string",
      "productDescription": "string",
      "quantity": 0,
      "unitPrice": 0
    }
    ...
  ]
}

Output I get:我得到的输出:

{
  "order": {
    "email": "string",
    "phone": "string",
    "company": "string",
    "firstName": "string",
    ...
  },
  "items": [
    {
      "description": "string",
      "orderLineId": 0,
      "priceType": "string",
      "productCode": "string",
      "productDescription": "string",
      "quantity": 0,
      "unitPrice": 0
    }
],
“0”: {
      "items": [
        {
          "description": "string",
          "orderLineId": 0,
          "priceType": "string",
          "productCode": "string",
          "productDescription": "string",
          "quantity": 0,
          "unitPrice": 0
        }
      ]
    }
...
}

Code used:使用的代码:

$client = array (
                    "order"=>array(
                        "email" => $order->get_billing_email(),
                        "phone" => $order->get_billing_phone(),
                        "company" => $order->get_billing_company(),
                        "firstName" => $order->get_billing_first_name(),
                    )
             );

       foreach ($order->get_items() as $item_key => $item ){
             $product = $item->get_product();
                           if(is_null($allitems)){
                                  $allitems = array (
                                        "items"=>array([
                                               "description" => $item->get_name(),
                                               "orderLineId"=>0,
                                               "priceType"=>"string",
                                               "productCode"=>$product->get_sku(),
                                               "productDescription" =>"string",
                                               "quantity" => $item->get_quantity(),
                                               "unitPrice"=> $product->get_price()
                                        ])
                                  );
                           }

                    else {
                           $singleitem = array (
                                  "items"=>array([
                                         "description" => $item->get_name(),
                                         "orderLineId"=>0,
                                         "priceType"=>"string",
                                         "productCode"=>$product->get_sku(),
                                         "productDescription" =>"string",
                                         "quantity" => $item->get_quantity(),
                                         "unitPrice"=> $product->get_price()
                                  ])
                           );
                           array_push($allitems, $singleitem);
                    }
             }

       # Combine client and items into one message
             $message = array_merge($client, $allitems);

I tried different methots of array_merge, array_push etc but somehow I cant get the items part the way it needs to be.我尝试了 array_merge、array_push 等的不同方法,但不知何故我无法按需要的方式获取项目。 Any suggestions on this?对此有何建议?

Your solution looks a bit messy and overcomplicated for a simple loop.对于一个简单的循环,您的解决方案看起来有点混乱和过于复杂。 It looks like you're re-declaring the items value instead of adding to an items array.看起来您正在重新声明 items 值而不是添加到 items 数组。 I think this should be closer to what you're after:我认为这应该更接近你所追求的:

$order_details = [
    "email" => $order->get_billing_email(),
    "phone" => $order->get_billing_phone(),
    "company" => $order->get_billing_company(),
    "firstName" => $order->get_billing_first_name(),
];

$all_items = [];
foreach( $order->get_items() as $item_key => $item ){
    $this_product = $item->get_product();
    $all_items[] = [
        "description" => $item->get_name(),
        "orderLineId"=>0,
        "priceType"=>"string",
        "productCode"=>$this_product->get_sku(),
        "productDescription" =>"string",
        "quantity" => $item->get_quantity(),
        "unitPrice"=> $this_product->get_price()
    ];
}

$message = [
    "order" => $order_details,
    "items" => $all_items,
];

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

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