【问题标题】:Merge JSON array according to key in PHP and extracting recurring keys根据PHP中的键合并JSON数组并提取重复键
【发布时间】:2017-02-09 20:53:46
【问题描述】:

我有一个 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 对数组进行排序,但我似乎无法提取重复键。

【问题讨论】:

  • 请展示你到目前为止所做的尝试
  • 我不知道如何在 PHP 中对其进行排序。这就是为什么我在这里问是否有人能指出我正确的方向

标签: php json merge key extract


【解决方案1】:

你可以走这条路:

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

更新

您生成的 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"
    }
]

【讨论】:

  • 你能告诉我如何在代码中做到这一点吗?我不是很熟练,这会很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
  • 2015-12-28
  • 2017-02-25
相关资源
最近更新 更多