I have two arrays and I need two create a seperate array or alter the first one with data from 2nd array. using similar fields with different names for reference. I need to search through array 2 till I find the root_server_url
field that matches with array 1 root_server_uri
field. once there I want to take the name
field from array 2 and add it to the end of array1 or separate array with all those fields. I think I might be over (or under) thinking this. both arrays are fed from two different json api calls. all of the URIs are unique.
Array 1
(
[8] => Array
(
[latitude] => 34.3025556
[longitude] => -77.4598012
[weekday_tinyint] => 4
[start_time] => 19:00:00
[meeting_name] => On Time
[root_server_uri] => https://example.com/server/unique
)
[16] => Array
(
[latitude] => 37.5050744
[longitude] => -73.5075403
[weekday_tinyint] => 4
[start_time] => 19:00:00
[meeting_name] => Robot
[root_server_uri] => http://www.example.org/server/
)
)
Array 2
(
[35] => Array
(
[url] => https://example.org/rest/v1/
[root_server_url] => https://example.com/server/unique
[name] => Kentucky
[num_regions] => 2
[num_areas] => 13
[num_meetings] => 548
[server_info] => [{"version": "2.10.7", "versionInt": "2010007}]
[last_successful_import] => 2018-10-15T20:00:12.952796Z
)
[36] => Array
(
[url] => https://example.org/rest/v1/
[root_server_url] => http://www.example.org/server/
[name] => San Jose
[num_regions] => 0
[num_areas] => 2
[num_meetings] => 145
[server_info] => [{"version": "2.10.5", "versionInt": "2010005"}]
[last_successful_import] => 2018-10-15T19:55:23.045540Z
)
)
I would like my final array to look like this Final Array
(
[8] => Array
(
[latitude] => 34.3025556
[longitude] => -77.4598012
[weekday_tinyint] => 4
[start_time] => 19:00:00
[meeting_name] => On Time
[root_server_uri] => https://example.com/server/unique
[name] => Kentucky
)
[16] => Array
(
[latitude] => 37.5050744
[longitude] => -73.5075403
[weekday_tinyint] => 4
[start_time] => 19:00:00
[meeting_name] => Robot
[root_server_uri] => http://www.example.org/server/
[name] => San Jose
)
)
this is the code I have so far, any help would be greatly appreciated.
$root_server = "https://example.org/rest/v1/";
$meetings_respone = get($root_server . "/json/&data_field_key=latitude,longitude,weekday_tinyint,start_time,meeting_name");
$meetings = json_decode($meetings_respone, true);
$rootServers_respone = get($root_server . "/rest/v1/rootservers/");
$rootServers = json_decode($rootServers_respone, true);
foreach ($unique_meetings as $meeting) {
foreach ($rootServers as $rootServer) {
if (strtolower($rootServer['root_server_url']) == $meeting['root_server_uri']) {
$name = $rootServer['name'];
}
}
echo '{latitude: ' . $meeting['latitude']
. ', longitude: ' . $meeting['longitude']
. ', weekday_tinyint: \'' . $meeting['weekday_tinyint']
. ' \', start_time: \'' . $meeting['start_time']
. '\', meeting_name: \'' . $meeting['meeting_name']
. '\', root_server_uri: \'' . $meeting['root_server_uri']
. '\', name: \'' . $name . '\''
. '},' . "
";
}
function get($url) {
error_log($url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$errorno = curl_errno($ch);
curl_close($ch);
if ($errorno > 0) {
throw new Exception(curl_strerror($errorno));
}
return $data;
}
This will be a lot easier if you reindex array 1 by the unique column 'root_server_uri'.
$array1 = array_column($array1, null, 'root_server_uri');
Then you can iterate array 2 and append the names using the matching keys.
foreach($array2 as $item) {
$array1[$item['root_server_url']]['name'] = $item['name'];
}
If there are multiple matching entries in array 2, you can append the names to an array at 'name' instead of assigning the value directly, so you can collect them all.
$array1[$item['root_server_url']]['name'][] = $item['name'];
If you need to keep the original keys from array 1, you can get a copy of them before reindexing with
$keys = array_keys($array1);
And then put them back after adding the names with
$array1 = array_combine($keys, $array1);
The keys and values should still match up because you're not adding/removing any items from array 1 or doing anything that will change the order.
If the original keys don't matter, you can remove the string keys with array_values
if you don't want them.
$array1 = array_values($array1);
I ended up using array search to find the unique key value in array 2 that matched array 1. from there I knew my matching keys and I could assign name from array2 properly to array 1.
foreach($array1 as $key => $value) {
$keyfind = array_search($array1[$key]['root_server_uri'], array_column($array2, 'root_server_url'));
$array1[$key]['name'] = $array2[$keyfind]['name'];
}
I'm sure there is a better way to do it, but it worked for me.