php - 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 等不同的方法,但不知何故,我无法按照需要的方式获取项目。对此有何建议?
解决方案
对于一个简单的循环,您的解决方案看起来有点混乱和过于复杂。看起来您是在重新声明 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,
];
推荐阅读
- excel - 分离一些数据
- javascript - 未捕获(承诺中)类型错误:无法读取未定义的属性“协议”
- angular - 找不到变量:Safari 中的 BigInt64Array
- node.js - 在节点 js 中实现推送提要
- javascript - combineLatest 重构 @deprecated — 不再支持 resultSelector,而是使用管道映射?
- c - 为什么这个算法来判断是整除还是行不通?
- javascript - 事件监听器后的 div 大小
- algorithm - 递归语句的运行时间
- r - 以闪亮的方式很好地安排情节子图
- python - 为什么我不能使用 requests 库访问网站,而我仍然可以从浏览器访问它?