latitude = 0; $average->longitude = 0; $average->inTime = 0; $average->parada = ''; $db_size = count($db); $distances=[]; $paradas = array( "Generales" => array(18.404916, -66.044828), "Natu_1" => array(18.403663, -66.045529), "Natu_2" => array(18.403769, -66.046572), "Centro" => array( 18.403898, -66.048255), "Biblio" => array(18.403994, -66.049781), "Merenderos" => array(18.406345, -66.050197), "Leyes" => array (18.406069, -66.047733), "Complejo" => array(18.406503, -66.046121), "ROTC_Entrada" => array(18.406083, -66.043605), "ROTC_Estacionamiento" => array( 18.407121, -66.042053) ); for ($i = 0; $i < $db_size; $i++) { $decoded_location = json_decode($db[$i]); $sum_of_lat += $decoded_location->latitude; $sum_of_long += $decoded_location->longitude; array_push($array_time,$decoded_location->inTime); if($i+1 == $db_size) { $average->latitude = $sum_of_lat/$db_size; $average->longitude = $sum_of_long/$db_size; $average->inTime = date("h:i:s a",(array_sum(array_map('strtotime',$array_time)))/$db_size); } } foreach($paradas as &$thing) { $plo = distanceMeters($average->latitude, $average->longitude, $thing[0], $thing[1]); array_push($distances,$plo); //echo $distances; } //echo $encoded_average; $indice = array_search(min($distances),$distances); $checking = array_keys($paradas); //$encoded_average = json_decode($encoded_avarage); $average->parada = $checking[$indice]; $encoded_average =json_encode($average); echo $encoded_average; //echo "pol"; //echo $average; ?>