如何在php中合并json数据中的数据

如何在php中合并json数据中的数据,php,json,multidimensional-array,grouping,Php,Json,Multidimensional Array,Grouping,我的数据看起来像: [{"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", "

我的数据看起来像:

[{"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"]}]

在json格式下操作数组数据是一项繁琐且通常容易出错的工作。首先要做的是将json转换为数组,这样您就可以使用php简单而强大的数组处理函数

您希望根据前三个元素值对数据进行分组。为了有效地实现这一点,您需要创建临时组合键——即临时关联键,其中包含作为字符串的组合值

这有助于您的代码确定您是否正在处理第一个事件。这是有价值的信息,因为您希望根据这些知识以不同的方式存储数据

代码:()

输出:(你不需要使用漂亮的印刷体,只是更容易阅读我的答案)

$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
[
    {
        "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"
        ]
    }
]