$sortarr = array(3,5,15,3,2,6,7,50,1,4,5,2,100,9,3,2,6,7,13,18);
echo "<pre>";
// Array to be sorted
print_r($sortarr);
// Sorted Array
print_r(bubble_sort($sortarr));
echo "<pre>";
function bubble_sort($sortarr){
// Bubble sorting
$array_count = count($sortarr);
for($x = 0; $x < $array_count; $x++){
for($a = 0 ; $a < $array_count - 1 ; $a++){
if($a < $array_count ){
if($sortarr[$a] > $sortarr[$a + 1] ){
swap($sortarr, $a, $a+1);
}
}
}
}
return $sortarr;
}
function swap(&$arr, $a, $b) {
$tmp = $arr[$a];
$arr[$a] = $arr[$b];
$arr[$b] = $tmp;
}
Implementasi:<?php
function getJarak($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000){
// convert from degrees to radians
$latFrom = deg2rad($latitudeFrom);
$lonFrom = deg2rad($longitudeFrom);
$latTo = deg2rad($latitudeTo);
$lonTo = deg2rad($longitudeTo);
$latDelta = $latTo - $latFrom;
$lonDelta = $lonTo - $lonFrom;
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
$jr=($angle * $earthRadius)/1000;
return round($jr,3);
}
function swap(&$arr, $a, $b) {
$tmp = $arr[$a];
$arr[$a] = $arr[$b];
$arr[$b] = $tmp;
}
?>
$i=0;
$j=0;
foreach($awisata -> result() as $a){
$dtelp=$a->telepon;
$dnama=$a->nama_tempat_wisata;
$dalamat=$a->alamat;
$dlat=$a->latitude;
$dlon=$a->longitude;
$arN[$i]=$dnama;
$arA[$i]=$dalamat;
$arLat[$i]=$dlat;
$arLon[$i]=$dlon;
$arLinkW[$j]=base_url('index/detail_info/wisata/'.$a->id_wisata);
$arNW[$j]=$dnama;
$arJW[$j]=getJarak($lat,$lon,$dlat,$dlon);$j++;
$i++;
}
$j=0;
foreach($ahiburan -> result() as $a){
$dtelp=$a->telepon;
$dnama=$a->nama_hiburan;
$dalamat=$a->alamat;
$dlat=$a->latitude;
$dlon=$a->longitude;
$arN[$i]=$dnama;
$arA[$i]=$dalamat;
$arLat[$i]=$dlat;
$arLon[$i]=$dlon;
$arNH[$j]=$dnama;
$arLinkH[$j]=base_url('index/detail_info/hiburan/'.$a->id_hiburan);
$arJH[$j]=getJarak($lat,$lon,$dlat,$dlon);$j++;
$i++;
}
$j=0;
foreach($atm -> result() as $a){
$dtelp=$a->telepon;
$dnama=$a->nama_tempat_makan;
$dalamat=$a->alamat;
$dlat=$a->latitude;
$dlon=$a->longitude;
$arN[$i]=$dnama;
$arA[$i]=$dalamat;
$arLat[$i]=$dlat;
$arLon[$i]=$dlon;
$arNM[$j]=$dnama;
$arLinkM[$j]=base_url('index/detail_info/tm/'.$a->id_tempat_makan);
$arJM[$j]=getJarak($lat,$lon,$dlat,$dlon);$j++;
$i++;
}
$j=0;
foreach($apenginapan -> result() as $a){
$dtelp=$a->telepon;
$dnama=$a->nama_penginapan;
$dalamat=$a->alamat;
$dlat=$a->latitude;
$dlon=$a->longitude;
$arN[$i]=$dnama;
$arA[$i]=$dalamat;
$arLat[$i]=$dlat;
$arLon[$i]=$dlon;
$arNP[$j]=$dnama;
$arLinkP[$j]=base_url('index/detail_info/penginapan/'.$a->id_penginapan);
$arJP[$j]=getJarak($lat,$lon,$dlat,$dlon);$j++;
$i++;
}
$j=0;
foreach($alayanan -> result() as $a){
$dtelp=$a->telepon;
$dnama=$a->nama_tempat_layanan;
$dalamat=$a->alamat;
$dlat=$a->latitude;
$dlon=$a->longitude;
$arN[$i]=$dnama;
$arA[$i]=$dalamat;
$arLat[$i]=$dlat;
$arLon[$i]=$dlon;
$arNL[$j]=$dnama;
$arLinkL[$j]=base_url('index/detail_info/layanan/'.$a->id_layanan);
$arJL[$j]=getJarak($lat,$lon,$dlat,$dlon);$j++;
$i++;
}
<?php
echo"<b>Taman Wisata Terdekat:</b>";
$array_count = count($arJW);
for($x = 0; $x < $array_count; $x++){
for($a = 0 ; $a < $array_count - 1 ; $a++){
if($a < $array_count ){
if($arJW[$a] > $arJW[$a + 1] ){
swap($arJW, $a, $a+1);
swap($arNW, $a, $a+1);
swap($arLinkW, $a, $a+1);
}
}
}
}//for $x
echo"<ol>";
for($i=1;$i<4;$i++){
echo "<li><a href='".$arLinkW[$i]."'>".$arNW[$i]." (".$arJW[$i]." KM) </a></li>";
}
echo"</ol>";
?>
Tidak ada komentar:
Posting Komentar