PHP如何按地点匹配规则合并两个数组/JSON数据
按地点合并配对JSON数据实现方案
需求梳理
需要合并两份JSON人员数据:一份存储type为1的人员数据,另一份存储type为0的人员数据,要求如下:
- 相同地点的数据归入同一大组
- 大组内按照配对规则拆分小分组:每2个及以上type1条目可搭配1个type0条目,反之同理,逻辑可类比往返航班配对
- 小分组内分别存放对应type的条目
输入数据示例
type=1 人员数据
[ { "id": 1, "name": "Jacob", "place": "Brazil", "type": 1 }, { "id": 2, "name": "Izac", "place": "Brazil", "type": 1 }, { "id": 3, "name": "Anran", "place": "Brazil", "type": 1 }, { "id": 4, "name": "Irbur", "place": "Brazil", "type": 1 }, { "id": 5, "name": "Lusos", "place": "Brazil", "type": 1 }, { "id": 6, "name": "Gamio", "place": "Brazil", "type": 1 }, { "id": 7, "name": "Nubeil", "place": "Brazil", "type": 1 }, { "id": 8, "name": "Usgon", "place": "Brazil", "type": 1 }, { "id": 15, "name": "Fikis", "place": "England", "type": 1 } ]
type=0 人员数据
[ { "id": 9, "name": "Gipin", "place": "Brazil", "type": 0 }, { "id": 10, "name": "Paoir", "place": "Brazil", "type": 0 }, { "id": 11, "cia": "Mutue", "place": "Brazil", "type": 0 }, { "id": 12, "name": "Ziefel", "place": "England", "type": 0 }, { "id": 13, "name": "Liedo", "place": "England", "type": 0 }, { "id": 14, "name": "Vicis", "place": "England", "type": 0 } ]
实现代码(JavaScript)
function mergeAndGroup(jsonType1, jsonType0) { // 第一步:按地点归集两类数据 const placeMap = {}; // 归集type1数据,移除type字段 jsonType1.forEach(item => { if (!placeMap[item.place]) { placeMap[item.place] = { type1: [], type0: [] }; } const { type, ...rest } = item; placeMap[item.place].type1.push(rest); }); // 归集type0数据,移除type字段 jsonType0.forEach(item => { if (!placeMap[item.place]) { placeMap[item.place] = { type1: [], type0: [] }; } const { type, ...rest } = item; placeMap[item.place].type0.push(rest); }); const result = { groups: [] }; // 第二步:按2:1规则拆分配对分组 Object.values(placeMap).forEach(({ type1, type0 }) => { let t1Pointer = 0, t0Pointer = 0; // 优先按数量多的一类做基准拆分 while (t1Pointer < type1.length || t0Pointer < type0.length) { const group = { type1: [], type0: [] }; // 取最多2个type1 for (let i = 0; i < 2 && t1Pointer < type1.length; i++) { group.type1.push(type1[t1Pointer++]); } // 取1个type0搭配 if (t0Pointer < type0.length) { group.type0.push(type0[t0Pointer++]); } // 如果type1取完了,按type0每2个搭配1个剩余的type1 if (group.type1.length === 0 && t0Pointer < type0.length) { for (let i = 0; i < 2 && t0Pointer < type0.length; i++) { group.type0.push(type0[t0Pointer++]); } if (t1Pointer < type1.length) { group.type1.push(type1[t1Pointer++]); } } result.groups.push(group); } }); return result; }
输出格式示例
{ "groups": [ { "type1": [ { "id": 1, "name": "Jacob", "place": "Brazil" }, { "id": 2, "name": "Izac", "place": "Brazil" } ], "type0": [ { "id": 9, "name": "Gipin", "place": "Brazil" } ] }, { "type1": [ { "id": 3, "name": "Anran", "place": "Brazil" }, { "id": 4, "name": "Irbur", "place": "Brazil" } ], "type0": [ { "id": 10, "name": "Paoir", "place": "Brazil" } ] }, { "type1": [ { "id": 5, "name": "Lusos", "place": "Brazil" }, { "id": 6, "name": "Gamio", "place": "Brazil" } ], "type0": [ { "id": 11, "cia": "Mutue", "place": "Brazil" } ] }, { "type1": [ { "id": 7, "name": "Nubeil", "place": "Brazil" }, { "id": 8, "name": "Usgon", "place": "Brazil" } ], "type0": [] }, { "type1": [ { "id": 15, "name": "Fikis", "place": "England" } ], "type0": [ { "id": 12, "name": "Ziefel", "place": "England" } ] }, { "type1": [], "type0": [ { "id": 13, "name": "Liedo", "place": "England" }, { "id": 14, "name": "Vicis", "place": "England" } ] } ] }
内容的提问来源于stack exchange,提问作者Jacob Oliver
相关产品推荐
相关产品推荐

