如何在php中的json数据中合并数据 [英] How To Merge Data inside of json data in php

查看:115
本文介绍了如何在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"}]

这是我的代码:

$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);
     }
}

运行我的代码后的结果:

my result after run by my code:

[{"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的简单而强大的数组处理功能.

Manipulating array data while in json format is a cumbersome and usually error-prone endeavor. The first thing to do is convert the json to an array so that you can employ php's simple and powerful array handling functions.

您要根据前三个元素值对数据进行分组.为了有效地做到这一点,您可以创建临时的组合键-即,临时组合键,其中包含组合值作为字符串.

You want to group the data based on the first three element values. To do this efficiently, you create temporary composite keys -- that is, temporary associative keys which contain the combined values as a string.

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

This helps your code to determine whether you are dealing with the first occurrence or not. This is valuable information because you wish to store data differently based on this knowledge.

代码:(演示)

$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,这更容易阅读我的答案)

Output: (you don't need to use PRETTY_PRINT, it's just easier to read my answer)

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

这篇关于如何在php中的json数据中合并数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆