duanbin198788 2018-10-15 22:28
浏览 201

迭代多个多维数组并输出json

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;
}
  • 写回答

2条回答 默认 最新

  • duangu4980 2018-10-15 22:48
    关注

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?