0

trying to calculate distance with lat and long and using pre-build php distance() function.Problem is that I am unable to formet array like this $point1 and $Point 2

   $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']); 

Here is my query and its return array like this

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

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

    )

this is code

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
}

Can I manage this out like $Point1 and $Point2

Thanks

3
  • 1
    try to print your $to and $from array, those should be same as $point1 and $point2. No need to push them in $finalDest Commented May 25, 2018 at 10:29
  • Nitin P@but distance function receive 4 value so can you correct this plzz Commented May 25, 2018 at 10:32
  • – Nitin P@update your answer so I can vote you thanks I got my result according your suggestion Commented May 25, 2018 at 10:36

1 Answer 1

1

Your code already has required arrays, try to print your $to and $from array, those should be same as $point1 and $point2. No need to push them in $finalDest

Your loop will look like this

    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;
                 }
Sign up to request clarification or add additional context in comments.

5 Comments

Nitin P@you there?
@newdeveloper : how may I help you
Nitin P@ $distance=DistanceCalculation($array1['lat'], $array1['long'],$array2['lat'],$array2['long']); @@how can return this value with return $usersInfo;
inside your for loop, use $usersInfo[$key]['distance'] = $distance;. Also change your array $key to something else, to avoid variable overriding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.