我有以下两个集合:
Collection {#402 ▼
#items: array:1 [▼
4007 => "4007 - Container Deposit - 18.00 Drum - In Stock: 0.00"
]
}
Collection {#398 ▼
#items: array:3 [▼
1000 => "1000 - Acetone - 162.00 KG - In Stock: 10000.00"
1001 => "1001 - Acetone - 15.80 KG - In Stock: 0.00"
24662 => "24662 - 1L Untd Antifreeze Orange FO2272A60(Prem - 1.00 Litre - In Stock: 0.00"
]
}使用Laravel的集合合并函数:
$merged = $ref_prod_containers->merge($ref_cust_prod);
dd($merged);我得到了以下信息:
Collection {#397 ▼
#items: array:4 [▼
0 => "4007 - Container Deposit - 18.00 Drum - In Stock: 0.00"
1 => "1000 - Acetone - 162.00 KG - In Stock: 10000.00"
2 => "1001 - Acetone - 15.80 KG - In Stock: 0.00"
3 => "24662 - 1L Untd Antifreeze Orange FO2272A60(Prem - 1.00 Litre - In Stock: 0.00"
]
}但是,我希望保留原始密钥。合并函数将它们移除并替换为0,1,2,3。
谢谢,朱利安
发布于 2017-02-10 18:45:28
您可以使用Laravel Collection的union()方法。注意,在处理重复键时,它的行为与merge()不同:如果$array1和$array2中都存在相同的键,并且您转到$merged = $array1->union($array2),那么$array1的值将在$merged集合中结束,而$array2的值将被丢弃(Laravel union documentation)。
发布于 2017-02-10 17:39:29
我会尝试对合并和合并的集合使用字符串键。从laravel docs部分合并集合,函数()
If the given array's keys are numeric, the values will be appended to the end of the collection:https://stackoverflow.com/questions/42149653
复制相似问题