具有键值的数组

时间:2018-05-25 10:23:59

标签: php codeigniter-3

尝试使用lat和long计算距离并使用预构建的php distance()函数。问题是我无法像这样的$ point1和$ Point 2 formet数组

   $point1 = array("lat" => "48.8666667", "long" => "2.3333333"); // Paris (France)
    $point2 = array("lat" => "19.4341667", "long" => "-99.1386111"); // Mexico City (Mexico)
    echo$km = distanceCalculation($point1['lat'], $point1['long'], $point2['lat'], $point2['long']); 

这是我的查询及其返回数组,如下所示

  Array
    (
        [0] => Array
            (
                [lat] => 32.9697
                [long] =>  -96.80322
            )

        [1] => Array
            (
                [lat] => 29.46786
                [long] =>  -98.53506
            )

    )

这是代码

foreach ($usersInfo as $key=> $users)
{
      $ridefrom=$users['ride_from'];
      $rideto=$users['ride_to'];//this is from lat long
      $tempLatLong = explode(',',$users['ride_from']);
       $tempLatLong1 = explode(',',$users['ride_to']);
      $key = array('lat','long');
      $to = array_combine($key,$tempLatLong); 
      $from = array_combine($key,$tempLatLong1);
      $array1=array_push($finalDest,$to);
      $array2=array_push($finalDest,$from);
echo $km = distanceCalculation($array1['lat'], $array1['long'],$array2['lat'],$array2['long']);
print_r($finalDest); // displays all array
}

我可以像$ Point1和$ Point2

那样管理它

由于

1 个答案:

答案 0 :(得分:1)

您的代码已经包含所需数组,请尝试打印您的$to$from数组,这些数组应与$point1$point2相同。无需在$finalDest

中推送它们

您的循环将如下所示

    foreach ($usersInfo as $index=> $users){
                      $km= 0;
                      $ridefrom=$users['ride_from'];
                      $rideto=$users['ride_to'];//this is from lat long
                      $tempLatLong = explode(',',$users['ride_from']);
                      $tempLatLong1 = explode(',',$users['ride_to']);
                      $key = array('lat','long');
                      $to = array_combine($key,$tempLatLong); 
                      $from = array_combine($key,$tempLatLong1);
                     $km=distanceCalculation($from['lat'],$from['long'],$to['lat'],$to['long']);
                      $usersInfo[$index]['distance'] = $km;
                 }