我有两个JSON对象,我将其转换为数组。我需要基于共享密钥(“locationCode”)加入两个数组,并将“地址”数组数据从一个数组添加到另一个数组。
使用CURL从远程服务器成功提取JSON后,我将它们转换为数组:
$json1 = json_decode($jsonresult, true);
$json2 = json_decode($jsonresult2, true);
生成的数组如下所示:
$ json1:
"maxResults":500,
"events":[
{
"eventCode":"20140001",
"eventId":"72",
"contact":{
"contactName":"John Doe",
"organization":"John Doe Inc.",
"notes":""
},
"location":{
"locationName":"Company Factory",
"locationCode":"factory",
"email":"",
"phone":"866-123-4567",
"tollfree":"",
"fax":"",
"url":"",
"notes":""
},
"timezone":"(GMT-08:00) Pacific Time (US & Canada)",
"primaryFormURL":"path/to/form"
},
{
"eventCode":"20140002",
"eventId":"73",
"contact":{
"contactName":"John Doe",
"organization":"John Doe Inc.",
"notes":""
},
"location":{
"locationName":"Company HQ",
"locationCode":"hq",
"email":"",
"phone":"866-123-4567",
"tollfree":"",
"fax":"",
"url":"",
"notes":""
},
"timezone":"(GMT-08:00) Pacific Time (US & Canada)",
"primaryFormURL":"path/to/form"
},
{
"eventCode":"20140003",
"eventId":"74",
"contact":{
"contactName":"John Doe",
"organization":"John Doe Inc.",
"notes":""
},
"location":{
"locationName":"Company HQ",
"locationCode":"factory",
"email":"",
"phone":"866-123-4567",
"tollfree":"",
"fax":"",
"url":"",
"notes":""
},
"timezone":"(GMT-08:00) Pacific Time (US & Canada)",
"primaryFormURL":"path/to/form"
}
]
$ json2:
"maxResults":500,
"locations":[
{
"numberOfRooms":null,
"totalSpace":null,
"address":{
"line1":"1245 Anystreet, Building 600",
"line2":"",
"line3":"",
"line4":"",
"city":"Anycity",
"state":"CA",
"postalCode":"98765",
"country":"United States",
"intlState":""
},
"locationCode":"factory",
"desc":"",
"url":""
},
{
"numberOfRooms":null,
"totalSpace":null,
"address":{
"line1":"3421 Anystreet, Building 200",
"line2":"",
"line3":"",
"line4":"",
"city":"Anycity",
"state":"CA",
"postalCode":"97654",
"country":"United States",
"intlState":""
},
"locationCode":"hq",
"desc":"",
"url":""
}
]
现在我需要根据“locationCode”键匹配加入两个数组。连接将包括将$ json2中的“地址”数组数据添加到适当匹配数组位置的$ json1中。我一直在尝试各种多维数组迭代器,但是我很难弄清楚如何实际选择性地将我需要的值从一个数组移动到另一个数组。我有一个找到匹配的迭代器,如下所示:
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($json1));
$iterator2 = new RecursiveIteratorIterator(new RecursiveArrayIterator($json2));
foreach($iterator as $key1=>$value1) {
if($key1=="locationCode") {
foreach($iterator2 as $key2=>$value2) {
if($key2=="locationCode" && $value1==$value2){
echo $key1.' -- '.$value1.':::'.$key2.' -- '.$value2.'<br />';
}
}
}
}
这成功输出匹配的值。我现在如何获取“地址”数组数据,并将其添加到$ json1中识别匹配的位置?
答案 0 :(得分:1)
我首先将json2处理成一个新的数组,其中locationCode值作为键,地址对象作为值。然后,如果你能找到这个数组中位置代码的匹配,我会遍历事件数组并添加地址。
$json1 = json_decode($jsonresult);
$json2 = json_decode($jsonresult2);
// map locations to associative array
$addresses = array();
foreach($json2['locations'] as $location) {
$addresses[$location->locationCode] = $location->address;
}
// add addresses to events
$events = $json1['events'];
array_walk($events, function (&$event, $key_not_used, $addresses) {
if(array_key_exists($event->locationCode, $addresses)) {
$event->address = $addresses[$event->locationCode];
}
});
OP更新: 这是最终的,有效的代码......
$json1 = json_decode($jsonresult);
$json2 = json_decode($jsonresult2);
// map locations to associative array
$addresses = array();
foreach($json2->locations as $currlocation) {
$addresses[$currlocation->locationCode] = $currlocation->address;
}
// add addresses to events
$events = $json1->events;
function add_address(&$event, $key_not_used, $searcharray) {
if(array_key_exists($event->location->locationCode, $searcharray)) {
$event->address = $searcharray[$event->location->locationCode];
}
}
array_walk($events, 'add_address', $addresses);
$merged_events = json_encode($events);
print_r($merged_events);