多维数组;加入;在PHP中,基于键值
我有两个JSON对象,我正在将它们转换为数组。我需要基于共享密钥(“locationCode”)连接两个数组,并将“address”数组数据从一个数组添加到另一个数组 使用CURL成功地从远程服务器提取JSON后,我将它们转换为数组:多维数组;加入;在PHP中,基于键值,php,json,multidimensional-array,left-join,Php,Json,Multidimensional Array,Left Join,我有两个JSON对象,我正在将它们转换为数组。我需要基于共享密钥(“locationCode”)连接两个数组,并将“address”数组数据从一个数组添加到另一个数组 使用CURL成功地从远程服务器提取JSON后,我将它们转换为数组: $json1 = json_decode($jsonresult, true); $json2 = json_decode($jsonresult2, true); 生成的数组如下所示: $json1: "maxResults":500, "events":[
$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"
}
]
"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":""
}
]
$json2:
"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"
}
]
"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 />';
}
}
}
}
$iterator=new recursiveiterator(new RecursiveArrayIterator($json1));
$iterator2=新的递归迭代器(新的递归数组迭代器($json2));
foreach($key1=>$value1的迭代器){
如果($key1==“位置代码”){
foreach($iterator2作为$key2=>$value2){
如果($key2==“locationCode”&&$value1==$value2){
回显$key1'.--'.$value1'.::'.$key2'.--'.$value2'.
;
}
}
}
}
这将成功输出匹配的值。现在如何获取“address”数组数据,并将其添加到$json1中识别匹配项的位置?我将首先将json2处理到一个新数组中,其中locationCode值作为键,address对象作为值。然后,我将遍历events数组,并将地址添加到该数组中,前提是您可以在该数组中找到位置代码的匹配项
$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];
}
});
操作更新:
这是最后的工作代码
$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);
匹配位置和地址的标准是什么?locationCode值将是相同的。我是否需要在json_解码函数中添加“、true”?我从来没有准确地理解返回对象中带/不带“true”的区别。@MaxwellDonnelly
true
选项将强制对象进入关联数组。我个人建议使用对象,因为当您将地址对象“复制”到事件时,实际上只是复制指向对象内存位置的指针,因此从内存管理的角度来看,这样做更好。在这种情况下,我还认为对象更能代表您在现实世界中使用的对象。您正在处理具有离散属性(地址行、城市、州等)的地址和具有自身属性的事件。您的代码让我非常接近。有几个语法错误我必须清理(缺少几个右括号),并且必须以稍微不同的方式访问对象。最后,我在array_walk本身之外编写了array_walk函数,并不得不重命名一些变量。我完成的工作代码将添加到您的答案中。