我有一个看起来像的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对数组进行排序,但我似乎无法提取重复键。
答案 0 :(得分:1)
您可以遵循这条道路:
service_type
存储在数组中。在每次迭代检查项目是否属于此值,然后推送到此集合service_type
service_prov_services
更新
您生成的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"
}
]