迭代通过两个嵌套数组在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
    }
}

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