PHP中基于键值的多维数组“ join”

麦克斯韦·唐纳利

我有两个要转换为数组的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中识别出匹配项的位置?

迈克·布兰特(Mike Brant)

我首先将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];
    }
});

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);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章