【发布时间】:2020-03-10 02:17:19
【问题描述】:
我有 2 个集合,第一个集合来自带有 where 条件的查询,而第二个集合来自所有项目。我打算合并这两个集合,但是在第一个数组和第二个数组之间有相同的项目。合并后如何使项目独一无二?
这是第一个数组,
[
{
"id": 71,
"id_brand": 1,
"id_brand_outlet": 14,
"id_user": 5,
"id_brand_outlet_tujuan": 15,
"alasan": "Try",
"no_surat": "JJ-00001-1",
"created_at": "2020-03-10 08:57:00",
"updated_at": "2020-03-10 08:57:00",
"type": "Delivery Out",
"category": "ITEM MASUK",
"brand_items": [
{
"id": 1,
"id_brand": 1,
"id_brand_delivery": 71,
"id_brand_item": 1,
"id_unit": 2,
"qty_before": 10,
"qty_change": 10,
"qty_after": 0,
"created_at": "2020-03-10 08:57:00",
"updated_at": "2020-03-10 08:57:00",
"name": "ALPUKAT",
"id_unit_opname": 2,
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
},
{
"id": 8,
"id_brand": 1,
"id_brand_delivery": 71,
"id_brand_item": 8,
"id_unit": 2,
"qty_before": 10,
"qty_change": 10,
"qty_after": 0,
"created_at": "2020-03-10 08:57:00",
"updated_at": "2020-03-10 08:57:00",
"name": "GULA AREN",
"id_unit_opname": 2,
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
}
]
}
]
这是第二个数组
[
{
"id": 1,
"id_owner": 3,
"name": "Janji Jiwa",
"slug": "JJ",
"logo": "images/brand/4041569999956.png",
"created_at": null,
"updated_at": "2020-03-05 16:02:11",
"category": "ITEM LAINNYA",
"brand_items": [
{
"id": 1,
"id_brand": 1,
"id_brand_category": 1,
"name": "ALPUKAT",
"accurate_name": "ALPUKAT",
"po_name": null,
"id_unit_order": 2,
"id_unit_opname": 2,
"id_unit2_order": null,
"id_unit2_opname": null,
"low_stock_limit": 0,
"price": 25000,
"minimum_order": 1,
"image": null,
"is_inventory": 1,
"is_ongkos_kirim": 0,
"limit": "2",
"created_at": "2020-02-25 09:32:46",
"updated_at": "2020-02-25 09:32:46",
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
},
{
"id": 3,
"id_brand": 1,
"id_brand_category": 8,
"name": "CHOCOLATE",
"accurate_name": "CHOCOLATE",
"po_name": null,
"id_unit_order": 2,
"id_unit_opname": 2,
"id_unit2_order": null,
"id_unit2_opname": null,
"low_stock_limit": 0,
"price": 25000,
"minimum_order": 1,
"image": null,
"is_inventory": 1,
"is_ongkos_kirim": 0,
"limit": null,
"created_at": "2020-02-25 09:32:47",
"updated_at": "2020-02-25 09:32:47",
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
},
{
"id": 8,
"id_brand": 1,
"id_brand_category": 9,
"name": "GULA AREN",
"accurate_name": "GULA AREN",
"po_name": null,
"id_unit_order": 2,
"id_unit_opname": 2,
"id_unit2_order": null,
"id_unit2_opname": null,
"low_stock_limit": 0,
"price": 12000,
"minimum_order": 1,
"image": null,
"is_inventory": 1,
"is_ongkos_kirim": 0,
"limit": null,
"created_at": "2020-02-25 09:32:48",
"updated_at": "2020-02-25 09:32:48",
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
},
{
"id": 9,
"id_brand": 1,
"id_brand_category": 12,
"name": "PLASTIC CUP 14 OZ",
"accurate_name": "PLASTIC CUP 14 OZ",
"po_name": null,
"id_unit_order": 2,
"id_unit_opname": 2,
"id_unit2_order": null,
"id_unit2_opname": null,
"low_stock_limit": 0,
"price": 17000,
"minimum_order": 1,
"image": null,
"is_inventory": 1,
"is_ongkos_kirim": 0,
"limit": null,
"created_at": "2020-02-25 09:32:48",
"updated_at": "2020-02-25 09:32:48",
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
},
{
"id": 11,
"id_brand": 1,
"id_brand_category": 8,
"name": "ROTI TAWAR",
"accurate_name": "ROTI TAWAR",
"po_name": null,
"id_unit_order": 2,
"id_unit_opname": 2,
"id_unit2_order": null,
"id_unit2_opname": null,
"low_stock_limit": 0,
"price": 25000,
"minimum_order": 1,
"image": null,
"is_inventory": 1,
"is_ongkos_kirim": 0,
"limit": null,
"created_at": "2020-02-25 09:32:48",
"updated_at": "2020-02-25 09:32:48",
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
},
{
"id": 14,
"id_brand": 1,
"id_brand_category": 13,
"name": "TISSUE GULUNG",
"accurate_name": "TISSUE GULUNG",
"po_name": null,
"id_unit_order": 2,
"id_unit_opname": 2,
"id_unit2_order": null,
"id_unit2_opname": null,
"low_stock_limit": 0,
"price": 17000,
"minimum_order": 1,
"image": null,
"is_inventory": 1,
"is_ongkos_kirim": 0,
"limit": null,
"created_at": "2020-02-25 09:32:48",
"updated_at": "2020-02-25 09:32:48",
"unit_opname": {
"id": 2,
"name": "Pcs",
"created_at": "2019-09-19 00:00:00",
"updated_at": "2019-09-19 00:00:00"
}
}
]
}
]
这是我的代码:
public function deliveryIn(Request $request){
$post = $request->all();
$data1 = BrandOutletDelivery::where('no_surat', 'LIKE' ,'%'.$post['no_surat'])->where('type', 'Delivery Out')->whereDate('created_at', date("Y-m-d"))->with('brandOutletDeliveryItems.unit')->get();
if(count($data1) > 0){
$data1[0]['category']="ITEM MASUK"; //add key & value to the array
foreach($data1 as $key => $value){
foreach ($value['brandOutletDeliveryItems'] as $key1 => $val) {
$value['brandOutletDeliveryItems'][$key1]['name'] = $val['brandItem']['name'];
$value['brandOutletDeliveryItems'][$key1]['id'] = $val['brandItem']['id'];
$value['brandOutletDeliveryItems'][$key1]['id_unit_opname'] = $val['brandItem']['id_unit_opname'];
$val['unit_opname'] = $val['unit'];
unset($val['unit']);
unset($val['brandItem']);
// $data2 = $data2->get();
}
$value['brand_items'] = $value['brandOutletDeliveryItems'];
unset($data1[$key]['brandOutletDeliveryItems']);
}
}
$data2 = Brand::where('id', $post['id_brand']);
$data2->with(['brandItems' => function($q){
$q->where('is_inventory', 1)->with('unitOpname');
}]);
$data2 = $data2->get();
// }
$data2[0]['category']="ITEM LAINNYA"; //add key & value to the array
$data = $data1->merge($data2);
return response(MyHelper::checkGet($data));
return ['status' => 'fail', 'messages' => ['Nomor Surat Tidak Ditemukan']];
}
在第一个数组中有brand_items,名称为“ALPUKAT”和“GULA AREN”,第二个数组中也有相同的项目。如何使第一个数组中的项目不再出现在第二个数组中?
我尝试过组合两个数组,但这些项目仍然不是唯一的。
【问题讨论】:
-
使用
in_array()编写一个函数,首先它遍历第一个数组来标识每个值,然后遍历第二个数组并比较删除双倍项的值。最后用删除的项目创建一个新数组。
标签: php arrays laravel eloquent