我的数据如下:
[{"REG":"SK", "RES":"PAN1", "WELL":"P1-TG", "TIME":"2005-03-27"},
{"REG":"SK", "RES":"PAN1", "WELL":"P1-TG", "TIME":"2005-04-13"},
{"REG":"SK", "RES":"PAN1", "WELL":"P1-TG", "TIME":"2006-06-07"},
{"REG":"SK", "RES":"PAN2", "WELL":"P2-TG", "TIME":"2009-01-18"},
{"REG":"SK", "RES":"PAN3", "WELL":"P3-TG", "TIME":"2009-03-01"},
{"REG":"SK", "RES":"PAN3", "WELL":"P3-TG", "TIME":"2010-03-14"}]
这是我的代码:
$query =[{my data}];
$list = [];
foreach($query as $index => $q){
if($index != 0){
$key = $index - 1;
$filter = [];
if($q->WELL == $query[$key]->WELL){
array_push($list,array_merge_recursive(json_decode(json_encode($q),true),json_decode(json_encode($query[$key]),true)));
}else{
array_push($list,$q);
}
}else{
array_push($list,$q);
}
}
运行我的代码后的结果:
[{"REG":"SK", "RES":"PAN1", "WELL":"P1-TG", "TIME":"2005-03-27"},
{"REG":["SK","SK"], "RES":["PAN1","PAN1"], "WELL":["P1-TG","P1-TG"], "TIME":["2005-03-27","2005-04-13"]},
{"REG":["SK","SK"], "RES":["PAN1","PAN1"], "WELL":["P1-TG","P1-TG"], "TIME":["2005-04-13","2006-06-07"]},
{"REG":"SK", "RES":"PAN2", "WELL":"P2-TG", "TIME":"2009-01-18"},
{"REG":"SK", "RES":"PAN3", "WELL":"P3-TG", "TIME":"2009-03-01"},
{"REG":["SK","SK"], "RES":["PAN3","PAN3"], "WELL":["P3-TG","P3-TG"], "TIME":["2009-03-01","2010-03-14"]}]
我想要的是:
[{"REG":"SK", "RES":"PAN1", "WELL":"P1-TG", "TIME":["2005-03-27","2005-04-13","2006-06-07"]},
{"REG":"SK", "RES":"PAN2", "WELL":"P2-TG", "TIME":"2009-01-18"},
{"REG":"SK", "RES":"PAN3", "WELL":"P3-TG", "TIME":["2009-03-01","2010-03-14"]}]
答案 0 :(得分:1)
以json格式处理数组数据是一项繁琐且通常容易出错的工作。首先要做的是将json转换为数组,以便您可以利用php的简单而强大的数组处理功能。
您要根据前三个元素值对数据进行分组。为了有效地做到这一点,您需要创建临时组合键-即临时组合键,其中包含组合值作为字符串。
这可以帮助您的代码确定您是否正在处理第一次出现的事件。这是有价值的信息,因为您希望基于此知识以不同的方式存储数据。
代码:(Demo)
$json = '[{"REG":"SK", "RES":"PAN1", "WELL":"P1-TG", "TIME":"2005-03-27"},
{"REG":"SK", "RES":"PAN1", "WELL":"P1-TG", "TIME":"2005-04-13"},
{"REG":"SK", "RES":"PAN1", "WELL":"P1-TG", "TIME":"2006-06-07"},
{"REG":"SK", "RES":"PAN2", "WELL":"P2-TG", "TIME":"2009-01-18"},
{"REG":"SK", "RES":"PAN3", "WELL":"P3-TG", "TIME":"2009-03-01"},
{"REG":"SK", "RES":"PAN3", "WELL":"P3-TG", "TIME":"2010-03-14"}]';
foreach (json_decode($json, true) as $row) { // convert json to array and iterate rows
$composite_key = implode('-', array_slice($row, 0, 3)); // group by the first 3 row values
if (!isset($result[$composite_key])) { // if 1st occurrence of first 3 row values
$row['TIME'] = [$row['TIME']]; // adjust last value in row to be a subarray
$result[$composite_key] = $row; // store the full data
} else {
$result[$composite_key]['TIME'][] = $row['TIME']; // push only the TIME value into the subarray
}
}
echo json_encode(array_values($result), JSON_PRETTY_PRINT); // re-index, convert to json and display
输出:(您无需使用PRETTY_PRINT,这更容易阅读我的答案)
[
{
"REG": "SK",
"RES": "PAN1",
"WELL": "P1-TG",
"TIME": [
"2005-03-27",
"2005-04-13",
"2006-06-07"
]
},
{
"REG": "SK",
"RES": "PAN2",
"WELL": "P2-TG",
"TIME": [
"2009-01-18"
]
},
{
"REG": "SK",
"RES": "PAN3",
"WELL": "P3-TG",
"TIME": [
"2009-03-01",
"2010-03-14"
]
}
]