搜索或排序多维数组以获取最接近的经度和经度值 [英] Search or sort multi dimensional array for closest lat and lon values

查看:81
本文介绍了搜索或排序多维数组以获取最接近的经度和经度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能很困难,我想根据lat和lon的最接近匹配搜索以下多维数组,然后返回该子数组.

This may be difficult, I want to search the following multi-dimensional array based on closest match of lat and lon and return that sub-array.

例如,我将已经设置以下值:

For example I will have the following values already set:

Lat = 50.6000,Lon = -74.15000

Lat=50.6000, Lon=-74.15000

我想搜索以下内容,并返回最匹配的数组.或按最接近的顺序对整个事物进行排序.

And I want to search the following, and return the array that is the closest match. Or sort the whole thing by closest to furthest.

更好的方法是对列表进行排序,以使最接近的经纬度首先降到最远.我不需要的是它以降序排序.例如,我需要最接近上述预设值.有人有主意吗?

Even better would be a way to just sort the list so the closest lat/lon are first down to the furthest away from. What I DON'T need is it sorted in descending order. I needs to be closest to the preset value above for example. Anyone have an idea?

Array
(
[0] => Array
    (
        [zipcode] => 28299
        [latitude] => 36.234982
        [longitude] => -81.913799
        [cityname] => ACME
    )

[1] => Array
    (
        [zipcode] => 17198
        [latitude] => 50.735880
        [longitude] => -74.143855
        [cityname] => NEWLAND
    )

[2] => Array
    (
        [zipcode] => 12203
        [latitude] => 41.711931
        [longitude] => -75.011806
        [cityname] => ACE
    )
)

推荐答案

此处的函数将执行此操作....但是,它返回包含2个主要元素latitudeslongitudes的多维数组.经度保存最近经度值的所有已排序条目,纬度"也保存最近经度值的已排序条目.默认情况下,该函数仅在纬度和纵向上返回最接近的匹配项.但是,将false作为最后一个参数传递将返回所有已排序的条目.下面的代码说明了这一切:

The Function here would do just that.... however, it returns a Multidimensional Array with with 2 major Elements latitudes and longitudes. The longitudes hold all the sorted Entries for the closest longitude values and the "latitudes" as well hold the sorted Entries for the closest latitude values. By default, the Function only returns the very Closest Match latitudinally and longitudinally. However, passing false as the last Parameter returns all sorted entries. The Code below illustrates it all:

<?php
    $arrGeoData = array(
        array(
            "zipcode"   => 28299,
            "latitude"  => 36.234982,
            "longitude" => -81.913799,
            "cityname"  => "ACME",
        ),
        array(
            "zipcode"   => 17198,
            "latitude"  => 50.735880,
            "longitude" => -74.143855,
            "cityname"  => "NEWLAND",
        ),
        array(
            "zipcode"   => 12203,
            "latitude"  => 41.711931,
            "longitude" => -75.011806,
            "cityname"  => "ACE",
        ),
    );
    $nearestLongLat     = sortByNearestLatLong($arrGeoData, 50.6000, -74.15000);
    var_dump(nearestLongLat);

    function sortByNearestLatLong($geoData, $lat, $long, $returnNearestOnly=true){
        // CREATE AN ARRAY FOR USE INSIDE THE FUNCTION
        $arrCloseMatchLat   = array();
        $arrCloseMatchLong  = array();
        $matchedGeoSet      = array();

        // LOOP THROUGH ALL THE $geoData ARRAY AND SUBTRACT THE GIVEN LAT & LONG VALUES
        // FROM THOSE CONTAINED IN THE ORIGINAL ARRAY: $geoData
        // WE KNOW THAT THE SMALLER THE RESULT OF THE SUBTRACTION; THE CLOSER WE ARE
        // WE DO THIS FOR BOTH THE LONGITUDE & LATITUDE... HENCE OUR ARRAY:
        // $arrCloseMatchLat AND $arrCloseMatchLong RESPECTIVELY
        foreach($geoData as $iKey=>$arrGeoStrip){
            $arrCloseMatchLat[$iKey]    =  abs(floatval( ($arrGeoStrip['latitude'])  - $lat  ));
            $arrCloseMatchLong[$iKey]   =  abs(floatval( ($arrGeoStrip['longitude']) - $long ));
        }


    // WE SORT BOTH ARRAYS NUMERICALLY KEEPING THE KEYS WHICH WE NEED FOR OUR FINAL RESULT
        asort($arrCloseMatchLat, SORT_NUMERIC);
        asort($arrCloseMatchLong, SORT_NUMERIC);

        // WE CAN RETURN ONLY THE RESULT OF THE FIRST, CLOSEST MATCH
        if($returnNearestOnly){
            foreach($arrCloseMatchLat as $index=>$difference){
                $matchedGeoSet['latitudes'][]  = $geoData[$index];
                break;
            }
            foreach($arrCloseMatchLong as $index=>$difference){
                $matchedGeoSet['longitudes'][] = $geoData[$index];
                break;
            }
            // OR WE CAN RETURN THE ENTIRE $geoData ARRAY ONLY SORTED IN A "CLOSEST FIRST" FASHION...
            // WE DO THIS FOR BOTH THE LONGITUDE & LATITUDE RESPECTIVELY SO WE END UP HAVING 2
            // ARRAYS: ONE THAT SORTS THE CLOSEST IN TERMS OF LONG VALUES
            // AN ONE THAT SORTS THE CLOSEST IN TERMS OF LAT VALUES...
        }else{
            foreach($arrCloseMatchLat as $index=>$difference){
                $matchedGeoSet['latitudes'][]  = $geoData[$index];
            }
            foreach($arrCloseMatchLong as $index=>$difference){
                $matchedGeoSet['longitudes'][] = $geoData[$index];
            }
        }
        return $matchedGeoSet;
    }

行: $ nearestLongLat = sortByNearestLatLong($ arrGeoData,50.6000,-74.15000); 上面的var_dump(nearestLongLat); 会产生类似下面的内容.注意纬度和经度键
The lines: $nearestLongLat = sortByNearestLatLong($arrGeoData, 50.6000, -74.15000); var_dump(nearestLongLat); above produces something like below. Notice the latitudes and longitudes keys

    array (size=2)
      'latitudes' => 
        array (size=1)
          0 => 
            array (size=4)
              'zipcode' => int 17198
              'latitude' => float 50.73588
              'longitude' => float -74.143855
              'cityname' => string 'NEWLAND' (length=7)
      'longitudes' => 
        array (size=1)
          0 => 
            array (size=4)
              'zipcode' => int 17198
              'latitude' => float 50.73588
              'longitude' => float -74.143855
              'cityname' => string 'NEWLAND' (length=7)

现在,我们使用不同的一组坐标再次尝试: $ nearestLongLat = sortByNearestLatLong($ arrGeoData,25.6000,-84.15000); var_dump($ nearestLongLat); .这将产生:
Now, we try again with a different set of Coordinates: $nearestLongLat = sortByNearestLatLong($arrGeoData, 25.6000, -84.15000); var_dump($nearestLongLat);. This produces:

array (size=2)
  'latitudes' => 
    array (size=1)
      0 => 
        array (size=4)
          'zipcode' => int 28299
          'latitude' => float 36.234982
          'longitude' => float -81.913799
          'cityname' => string 'ACME' (length=4)
  'longitudes' => 
    array (size=1)
      0 => 
        array (size=4)
          'zipcode' => int 28299
          'latitude' => float 36.234982
          'longitude' => float -81.913799
          'cityname' => string 'ACME' (length=4)

这篇关于搜索或排序多维数组以获取最接近的经度和经度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆