latitude = 0; $average->longitude = 0; $average->inTime = 0; $average->parada = ''; $average2 = new stdClass; $average2->latitude = 0; $average2->longitude = 0; $average2->inTime = 0; $average2->parada = ''; $db_size = count($db); $db2_size = count($db2); $distances=[]; $distances2 = []; $paradas = array( "Generales" => array(18.404916, -66.044828), "Natu1" => array(18.403663, -66.045529), "Natu2" => 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_EntrR" => array(18.406083, -66.043605), "rotc_Est" => array( 18.407121, -66.042053), "Museo" => array(18.403917, -66.050996), "FacundaB" => array(18.401992, -66.050611), "Educacion" => array(18.402077, -66.047800), "MultiP" => array(18.402133, -66.045014) ); 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); } } for ($i = 0; $i < $db2_size; $i++) { $decoded_location2 = json_decode($db2[$i]); $sum_of_lat2 += $decoded_location2->latitude; $sum_of_long2 += $decoded_location2->longitude; array_push($array_time2,$decoded_location2->inTime); if($i+1 == $db2_size) { $average2->latitude = $sum_of_lat2/$db2_size; $average2->longitude = $sum_of_long2/$db2_size; $average2->inTime = date("h:i:s a",(array_sum(array_map('strtotime',$array_time2)))/$db2_size); } } foreach($paradas as &$coso) { $plo2 = distanceMeters($average2->latitude, $average2->longitude, $coso[0], $coso[1]); array_push($distances2,$plo2); //echo $distances; } foreach($paradas as &$thing) { $plo = distanceMeters($average->latitude, $average->longitude, $thing[0], $thing[1]); array_push($distances,$plo); //echo $distances; } $indice = array_search(min($distances),$distances); $indice2 = array_search(min($distances2),$distances2); $checking = array_keys($paradas); $average->parada = $checking[$indice]; $average2->parada = $checking[$indice2]; $encoded_average = json_encode($average); $encoded_average2 = json_encode($average2); // echo $encoded_average; $encoded_average = json_decode($encoded_average); $encoded_average2 = json_decode($encoded_average2); if (!empty($encoded_avarage2)){ $jsoncito = lcfirst($encoded_average->parada) . "_" . ucfirst($encoded_average->parada); } elseif(!empty($encoded_average)){ $jsoncito = lcfirst($encoded_average2->parada) . "_" . ucfirst($encoded_average2->parada); } else{ $jsoncito = lcfirst($encoded_average->parada) . "_" . ucfirst($encoded_average2->parada); } $paquete -> parada = $jsoncito; date_default_timezone_set('America/Puerto_Rico'); $currenttime = date('h:i:s'); $paquete -> tiempo = $currenttime; $encoded_paquete = json_encode($paquete); echo $encoded_paquete; ?>