根据PHP中的键合并JSON数组并提取重复键

时间:2016-10-01 10:52:44

标签: php json merge key extract

我有一个看起来像的JSON数组:

{
"error": false,
"service_prov_services": [
    {
        "service_measure": "Darmkrebsfrüherkennung (Schnelltest, Koloskopie)",
        "service_prov_type": "Doctor",
        "service_type": "Früherkennung und Vorsorge",
        "service_prov_name": "Dr. Test",
        "addr_street": "xxx Street"
    },
    {
        "service_measure": "Gesundheits-Check-up",
        "service_prov_type": "Doctor",
        "service_type": "Früherkennung und Vorsorge",
        "service_prov_name": "Dr. Test",
        "addr_street": "xxx Street"
    },

    {
        "service_measure": "Suchtmittelkonsum",
        "service_prov_type": "Doctor",
        "service_type": "Gesundheitskurse",
        "service_prov_name": "Dr. Test",
        "addr_street": "xxx Street"
    },
    {
        "service_measure": "Stressbewältigung oder Entspannung",
        "service_prov_type": "Doctor",
        "service_type": "Gesundheitskurse",
        "service_prov_name": "Dr. Test",
        "addr_street": "xxx Street"
    },
    {
        "service_measure": "Schutzimpfung",
        "service_prov_type": "Doctor",
        "service_type": "Sport und Gesundheit",
        "service_prov_name": "Dr. Test",
        "addr_street": "xxx Street"
    }
]
}

我希望它是以下格式的json数组。 应提取重复出现的密钥值,并且只应发送一次。

    {
        "error": false,
        "service_prov_type": "Doctor",
        "service_prov_name": "Dr. Test",
        "addr_street": "xxx Street",
        "service_prov_services": [
          {
               "service_type": "Früherkennung und Vorsorge",
               "service_measure": "Darmkrebsfrüherkennung
                             (Schnelltest, Koloskopie)","Gesundheits-
                             Check-up"
           },
           {
               "service_type": "Gesundheitskurse",
               "service_measure": "Suchtmittelkonsum", 
                           "Stressbewältigung oder Entspannung"
           },
           {
               "service_type": "Sport und Gesundheit",
               "service_measure": "Schutzimpfung",
           }
        ]
    }

有人能指出怎么做吗? 我可以使用usort对数组进行排序,但我似乎无法提取重复键。

1 个答案:

答案 0 :(得分:1)

您可以遵循这条道路:

  1. 使用json_decode
  2. 将json转换为PHP数组
  3. 遍历密钥$ service_prov_services。在课程中:
    a)将service_type存储在数组中。在每次迭代检查项目是否属于此值,然后推送到此集合
    b)如果没有,那么创建新的集合并推进其中。
    1. 在循环结束时,您将拥有service_type
    2. 的集合
    3. service_prov_services
    4. 中使用此集合
    5. 使用json_encode
    6. 将数组转换回json
  4. 更新

    您生成的JSON将service_measure作为字符串而不是数组。我的代码将其作为数组。除了将UTF8密钥存储在映射器中之外,我遵循了完全相同的步骤。

    <?php
    
    ini_set('display_errors', 'On');
    error_reporting(E_ALL);
    
    $json = <<<EOF
    {
        "error": false,
        "service_prov_services": [
            {
                "service_measure": "Darmkrebsfr?herkennung (Schnelltest, Koloskopie)",
                "service_prov_type": "Doctor",
                "service_type": "Fr?herkennung und Vorsorge",
                "service_prov_name": "Dr. Test",
                "addr_street": "xxx Street"
            },
            {
                "service_measure": "Gesundheits-Check-up",
                "service_prov_type": "Doctor",
                "service_type": "Fr?herkennung und Vorsorge",
                "service_prov_name": "Dr. Test",
                "addr_street": "xxx Street"
            },
            {
                "service_measure": "Suchtmittelkonsum",
                "service_prov_type": "Doctor",
                "service_type": "Gesundheitskurse",
                "service_prov_name": "Dr. Test",
                "addr_street": "xxx Street"
            },
            {
                "service_measure": "Stressbew?ltigung oder Entspannung",
                "service_prov_type": "Doctor",
                "service_type": "Gesundheitskurse",
                "service_prov_name": "Dr. Test",
                "addr_street": "xxx Street"
            },
            {
                "service_measure": "Schutzimpfung",
                "service_prov_type": "Doctor",
                "service_type": "Sport und Gesundheit",
                "service_prov_name": "Dr. Test",
                "addr_street": "xxx Street"
            }
        ]
    }
    EOF;
    
    $listArr = json_decode($json, TRUE);
    
    $service_type = array();
    $service_prov_services = array();
    
    $mapper = array();
    $entry_list = array();
    $mapIndex = 0;  //initialize map index at zero
    
    
    foreach($listArr['service_prov_services'] as $collection){
    
        if(in_array($collection['service_type'], array_values($mapper))){
                //make edit to existing entry by pusing service_measure
    
                //extract the index from mapper now
                foreach($mapper as $key=>$value){
                    if($value == $collection['service_type']){
                        $needle = $key;
                        break;
                    }
                }
    
                $arr_service_measure_old = (array)$entry_list[$needle]['service_measure'];
                $arr_service_measure_old[] = $collection['service_measure'];
    
                //update the array
                $entry_list[$needle] = array(
                    'service_type'      =>  $collection['service_type'],
                    'service_measure'   =>  $arr_service_measure_old,
                );
    
        }else{
                //new entry needs to be made
                $currentMapIndex = $mapIndex;
                $mapper[$mapIndex++] = $collection['service_type'];
    
                $entry_list[$currentMapIndex] = array(
                    'service_type'      =>  $collection['service_type'],
                    'service_measure'   =>  $collection['service_measure'],
                );
    
        }
    
    }
    
    echo json_encode($entry_list);
    

    结果JSON是:

    [
        {
            "service_type": "Fr?herkennung und Vorsorge",
            "service_measure": [
                "Darmkrebsfr?herkennung (Schnelltest, Koloskopie)",
                "Gesundheits-Check-up"
            ]
        },
        {
            "service_type": "Gesundheitskurse",
            "service_measure": [
                "Suchtmittelkonsum",
                "Stressbew?ltigung oder Entspannung"
            ]
        },
        {
            "service_type": "Sport und Gesundheit",
            "service_measure": "Schutzimpfung"
        }
    ]