doq70020 2014-08-19 20:02
浏览 69
已采纳

迭代通过两个嵌套数组在php中进行数据比较

Alright everyone, I have two arrays that look like this:

array(3) {
  ["15PCW"]=>
  array(4) {
    ["longitude"]=>
    string(17) "-88.3511517556820"
    ["latitude"]=>
    string(16) "35.9298720640290"
    ["knownLocationTag"]=>
    string(7) "15PCW"
    ["count"]=>
    string(1) "0"
  }
  ["12RCD"]=>
  array(4) {
    ["longitude"]=>
    string(17) "-88.4423244521810"
    ["latitude"]=>
    string(16) "34.0964334531290"
    ["knownLocationTag"]=>
    string(6) "12RCD"
    ["count"]=>
    string(1) "0"
  }
  ["43RICK"]=>
  array(4) {
    ["longitude"]=>
    string(17) "-88.5154700586220"
    ["latitude"]=>
    string(16) "34.0726750453080"
    ["knownLocationTag"]=>
    string(11) "43RICK"
    ["count"]=>
    string(1) "0"
  }
}


array(3) {
  [12]=>
  array(4) {
    ["entryID"]=>
    string(2) "12"
    ["longitude"]=>
    string(17) "-88.35108453430"
    ["latitude"]=>
    string(16) "32.9298345948310"
    ["knownLocationTag"]=>
    NULL
  }
  [13]=>
  array(4) {
    ["entryID"]=>
    string(2) "13"
    ["longitude"]=>
    string(17) "-88.3513437005120"
    ["latitude"]=>
    string(16) "32.9458453856350"
    ["knownLocationTag"]=>
    NULL
  }
  [14]=>
  array(4) {
    ["entryID"]=>
    string(2) "14"
    ["longitude"]=>
    string(17) "-88.3511544967700"
    ["latitude"]=>
    string(16) "32.9293468765800"
    ["knownLocationTag"]=>
    NULL
  }
}

I want to use PHP to iterate through each of the second set of arrays and run a function including data from both arrays (in this case we are checking the distance between two GPS locations) and if they are within x miles of one of the items in the list in the first array it increments the "count" value or if they are outside of that set range it adds that value to the first array.

So far my solution is just 4 nested foreach loops and it is getting messy. Is that the right way to go about it or is there a better way?

foreach ($devices as $devicesKey => $devicesValue)
{
    foreach ($devicesValue as $devicesKey2 => $devicesValue2)
    {
        $testarray[$devicesKey2] = $devicesValue2;
    }
    foreach ($locations as $locationsKey => $locationsValue)
{
    foreach ($locationsValue as $locationsKey2 => $locationsValue2)
    {
        $testarray2[$locationsKey2] = $locationsValue2;
    }
}
    $test =  distance($testarray['longitude'],$testarray['latitude'],$testarray2['longitude'],$testarray2['latitude']);
    if ($test < .26)
    {
        //we would incriment the counter here since we match
    }
    else
    {
        //we would add the value to the array here since we are outside the bounds
    }
}
  • 写回答

1条回答 默认 最新

  • dongyong8491 2014-08-19 20:45
    关注

    As messy as it seems, its probably the right way to go about it. You could clean up the code by creating a evaluateDevice function, whereby for each element in the second list, you pass it to the function, which is then responsible for processing it against your first list

    foreach($devices as  $key => $outerDevice){
        foreach($outerDevice as $device){
            evaluateDevice($outerDevice, $device, $key);
        }
    }
    
    function evaluateDevice(array $outerDevice, array $device, $key)
    {
        foreach($locations as $outerLocation){
            foreach($OuterLocation as $location){
                $distance = distance($device['longitude'], $device['latitude'], $location['longitude'], $location['latitude']);
    
                if($distance < .26){
                    // increment count
                }else{
                    // remove from second list and add to first
                    $locations['12ABC"] = $device;
                    unset($outerDevice[$key]);
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器