将两个多维数组与回调进行比较

时间:2022-07-23 12:14:33

I have two arrays that look like this:

我有两个看起来像这样的数组:

array(1) {
  ["x8hQz"]=>
  array(1) {
    ["2014-07-16"]=>
    array(20) {
      [0]=>
      array(2) {
        ["latitude"]=>
        string(16) "39.9298775948310"
        ["longitude"]=>
        string(17) "-87.3510829502270"
      }
      [1]=>
      array(2) {
        ["latitude"]=>
        string(16) "39.9298960856350"
        ["longitude"]=>
        string(17) "-87.3511097005120"
      }...

So each array contains a deviceID, list of dates, and a list of GPS locations for each date. Each day may have a differing number of GPS locations. The arrays are identical and I want to compare the array to itself and remove days/GPS location groups based on how well they match other days in the array so that when I am done one of the arrays only contains days that have unique lists of locations on a per device basis. I want to compare the days within the arrays and if each of the value pairs within a day are within x number of feet based on the GPS locations then I would discard the matching days data.

因此,每个数组都包含一个deviceID,日期列表以及每个日期的GPS位置列表。每天可能有不同数量的GPS位置。阵列是相同的,我想比较阵列与自身,并根据它们与阵列中其他日期的匹配程度删除天/ GPS位置组,这样当我完成时,其中一个阵列只包含具有唯一位置列表的天数在每个设备的基础上。我想比较数组中的天数,如果一天内的每个值对都在x个英尺的范围内,那么我会丢弃匹配的天数据。

I was planning on doing a foreach through both arrays but as I have it setup now I only have one keypair for one day to compare all of the other keypairs to so I am not able to compare the entire day to the list of days in the other array.

我正计划通过两个阵列做一个f​​oreach,但是现在我已经设置好了,我只有一天的密钥对来比较所有其他密钥对,所以我无法将整天记录到日期列表中。其他阵列。

foreach ($trackArray as $deviceKey => $deviceValue)
{
    //echo $deviceKey . $deviceValue . "\n";
    foreach ($deviceValue as $dateKey => $dateValue)
    {
        //echo $dateKey . $dateValue . "\n";
        foreach ($dateValue as $locationKey => $locationValue)
        {
            //echo $locationKey . $locationValue . "\n";
            //echo $trackArray[$deviceKey][$dateKey][$locationKey]['latitude'];
            foreach ($finalTrackArray[$deviceKey] as $finalDateKey => $finalDateValue)
            {
                //echo $finalDateKey . $finalDateValue . "\n";
                ...
            }
        }
    }
}

1 个解决方案

#1


Very Impractical and Processing-Intensive but here is the solution to your first part of the question.

非常不切实际和处理密集型,但这是您问题第一部分的解决方案。

The arrays are identical and I want to compare the array to itself and remove days/GPS location groups based on how well they match other days in the array so that when I am done one of the arrays only contains days that have unique lists of locations on a per device basis

阵列是相同的,我想比较阵列与自身,并根据它们与阵列中其他日期的匹配程度删除天/ GPS位置组,这样当我完成时,其中一个阵列只包含具有唯一位置列表的天数在每个设备的基础上

The Code

$finalTrackArray = []; 
foreach ($trackArray as $deviceKey => $deviceValue) { 
    foreach ($deviceValue as $dateKey => $dateValue) { 
        foreach ($dateValue as $locationValue) { 
            $serializedLocationList[] = serialize($locationValue); # Collect serialized version of all location value: latitude and longitude
        }
        foreach (array_unique($serializedLocationList, SORT_STRING) as $serializedLocationValue) { 
            $finalTrackArray[$deviceKey][$dateKey][] = unserialize($serializedLocationValue);
        }
    }
} 
//echo '<pre>'.print_r($finalTrackArray,1).'</pre>';

#1


Very Impractical and Processing-Intensive but here is the solution to your first part of the question.

非常不切实际和处理密集型,但这是您问题第一部分的解决方案。

The arrays are identical and I want to compare the array to itself and remove days/GPS location groups based on how well they match other days in the array so that when I am done one of the arrays only contains days that have unique lists of locations on a per device basis

阵列是相同的,我想比较阵列与自身,并根据它们与阵列中其他日期的匹配程度删除天/ GPS位置组,这样当我完成时,其中一个阵列只包含具有唯一位置列表的天数在每个设备的基础上

The Code

$finalTrackArray = []; 
foreach ($trackArray as $deviceKey => $deviceValue) { 
    foreach ($deviceValue as $dateKey => $dateValue) { 
        foreach ($dateValue as $locationValue) { 
            $serializedLocationList[] = serialize($locationValue); # Collect serialized version of all location value: latitude and longitude
        }
        foreach (array_unique($serializedLocationList, SORT_STRING) as $serializedLocationValue) { 
            $finalTrackArray[$deviceKey][$dateKey][] = unserialize($serializedLocationValue);
        }
    }
} 
//echo '<pre>'.print_r($finalTrackArray,1).'</pre>';