【问题标题】:Iterate through two nested arrays for data comparison in php在php中遍历两个嵌套数组进行数据比较
【发布时间】:2014-08-19 20:02:12
【问题描述】:

大家好,我有两个如下所示的数组:

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
  }
}

我想使用 PHP 遍历第二组数组中的每一个并运行一个函数,该函数包括来自两个数组的数据(在这种情况下,我们正在检查两个 GPS 位置之间的距离)以及它们是否在一个 x 英里范围内对于第一个数组中列表中的项目,它会增加“计数”值,或者如果它们超出该设置范围,则将该值添加到第一个数组中。

到目前为止,我的解决方案只有 4 个嵌套的 foreach 循环,而且越来越混乱。这是正确的方法还是有更好的方法?

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
    }
}

【问题讨论】:

    标签: php multidimensional-array


    【解决方案1】:

    虽然看起来很混乱,但它可能是正确的方法。您可以通过创建evaluateDevice 函数来清理代码,从而对于第二个列表中的每个元素,您将其传递给该函数,然后该函数负责根据您的第一个列表对其进行处理

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 2020-01-17
      • 1970-01-01
      相关资源
      最近更新 更多