I have two JSON objects which I am converting to arrays. I need to join the two arrays based on a shared key ("locationCode"), and add the "address" array data from one array to the other.
After I successfully pull the JSON from a remote server using CURL, I convert them to arrays:
$json1 = json_decode($jsonresult, true);
$json2 = json_decode($jsonresult2, true);
The resulting arrays look like this:
$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":""
}
]
Now I need to join the two arrays based on the "locationCode" key matching. The join will consist of adding the "address" array data from $json2 into $json1 in the appropriate matched array position. I have been trying a variety of multidimensional array iterators, but I'm having a hard time figuring out how to actually selectively move the values I need from one array to the other. I have an iterator that finds the matches, like so:
$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 />';
}
}
}
}
This successfully outputs the matched values. How do I now grab the "address" array data, and add it to the position within $json1 where the match was identified?