首页 > 解决方案 > PHP在数组内组合数组

问题描述

我正在编写一个 API 并将数据发送到应用程序,我需要一些以某种方式格式化的数组,我似乎无法正确处理。

该消息存在于两个数组中:Order 和 Items。Order 是单个数组,Items 似乎是数组中的一个数组,当订购多个项目时会出错。

所需格式:

    {
  "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
    }
    ...
  ]
}

我得到的输出:

{
  "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
        }
      ]
    }
...
}

使用的代码:

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

我尝试了 array_merge、array_push 等不同的方法,但不知何故,我无法按照需要的方式获取项目。对此有何建议?

标签: phparrays

解决方案


对于一个简单的循环,您的解决方案看起来有点混乱和过于复杂。看起来您是在重新声明 items 值,而不是添加到 items 数组。我认为这应该更接近你所追求的:

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

推荐阅读