简体   繁体   中英

How to find min&max in array push

I am doing a api for cab booking app like uber.I am using distancematrix to find the distance between two latitude and longitude.In that single customer have multiple drivers around it.I would like to find nearest driver using single customer latitude and longitude over multiple driver latitude and longitude.How can i find min&max distance of driver and get min&max value based on driver id.

Below code i am using to find the nearest driver:

<?php
        function GetDrivingDistance($lat1, $lat2, $long1, $long2)
        {
            $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&mode=driving&language=pl-PL";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            $response = curl_exec($ch);
            curl_close($ch);
            $response_a = json_decode($response, true);
            $dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
            $time = $response_a['rows'][0]['elements'][0]['duration']['text'];

            return array('distance' => $dist, 'time' => $time);
            //return $distance;
        }

    $cus_latlong_qry="SELECT * FROM customer_position WHERE customer_id='$customer_id'";
    $cus_latlong_exe=mysqli_query($con,$cus_latlong_qry);
    $cus_latlong_res=mysqli_fetch_array($cus_latlong_exe);
    $cus_lat=$cus_latlong_res['latitude'];
    $cus_long=$cus_latlong_res['longtitude'];

$driver_latlong_qry="SELECT * FROM driver_position WHERE driver_type='$_GET[type_id]' AND (booking_status='3' OR booking_status='2') AND active_status='0'";

                $driver_latlong_exe=mysqli_query($con,$driver_latlong_qry);
                $minimumvalue=array();
                $distance=array();
                while($driver_latlong_res=mysqli_fetch_array($driver_latlong_exe))
                {
                    $driv_id=$driver_latlong_res['driver_id'];
                    $driv_lat=$driver_latlong_res['lattitude'];
                    $driv_long=$driver_latlong_res['longtitude'];
                    $dist = GetDrivingDistance($cus_lat,$driv_lat,$cus_long,$driv_long);
                    array_push($distance,$dist);
                }

?>

here how can i find min&mx distance in array push with respect to particular driver id..??

Simple logic, collect min & max values:

$drivers = array();    

 while($driver_latlong_res=mysqli_fetch_array($driver_latlong_exe))
 {
  $driv_id=$driver_latlong_res['driver_id'];                  
  $driv_lat=$driver_latlong_res['lattitude'];               
  $driv_long=$driver_latlong_res['longtitude']; 
  $dist = GetDrivingDistance($cus_lat,$driv_lat,$cus_long,$driv_long);

  array_push($distance[$driv_id], $dist);

  $drivers[$driv_id]['min'] = min($distance[$driv_id]);     
  $drivers[$driv_id]['max'] = max($distance[$driv_id]);

 }

print_r($drivers);

As a result you will get $drivers array where first key is driver_id, and as a second key you will have min and max values for him.

use min & max enter code here

min($distance[$driv_id]);     
max($distance[$driv_id]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM