3v4l.org

run code in 300+ PHP versions simultaneously
<?php $hotels = [ ['Ingles', 1, 8.0, 34026], ['Auto', 3, 8.2, 34247], ['Pincipal', 3, 8.0, 34787], ['Akto', 3, 8.1, 33551], ['Caledonian', 3, 8.1, 37579], ['Catalunia', 2, 8.2, 38690], ['Aktabecene', 2, 8.0, 39010], ['Sunotel', 4, 8.0, 39023], ['Gondal', 2, 8.0, 40009], ['Chik', 3, 8.2, 40817], ['Catalonia Avino', 3, 8.3, 41402], ['Kami', 0, 8.8, 41450], ['Konstantsa', 2, 8.8, 41938], ['Room', 3, 8.8, 42102], ['Redjesia', 3, 8.7, 42735], ]; $totalHotelsNumber = 2; $starCoef = 0.2; $ratingCoef = 2000; for ( $i=0; $i<count($hotels); $i++ ) { $hotels[$i][4] = 0; } for ( $i=0; $i<count($hotels); $i++ ) { for ( $j=$i+1; $j<count($hotels); $j++ ) { echo 'Comparing '.$hotels[$i][0].' and '.$hotels[$j][0].'...'."\r\n"; $starDifference = $hotels[$i][1]-$hotels[$j][1]; $ratingToCompensate = $starDifference*$starCoef; $rating = $hotels[$i][2]+$ratingToCompensate; $ratingDifference = ($rating-$hotels[$j][2])*10; $priceToCompensate = $ratingDifference*$ratingCoef; $price = round($hotels[$i][3]-$priceToCompensate); echo $price.' VS '.$hotels[$j][3]."\r\n"; if ( $price < $hotels[$j][3] ) { echo $hotels[$i][0].' wins!'."\r\n"; $hotels[$i][4]++; } else { echo $hotels[$j][0].' wins!'."\r\n"; $hotels[$j][4]++; } echo "\r\n".'----------'."\r\n"; } } usort($hotels, function($hotelA, $hotelB){ return $hotelB[4]-$hotelA[4]; }); echo "\r\n".'----------'."\r\n"; echo 'Top:'."\r\n\r\n"; for ( $i=0; $i<count($hotels); $i++ ) { echo $hotels[$i][0].': '.$hotels[$i][4]."\r\n"; } echo "\r\n".'----------'."\r\n"; echo 'Finalists:'."\r\n\r\n"; for ( $i=0; $i<=1; $i++ ) { echo $hotels[$i][0]."\r\n"; }

preferences:
64.57 ms | 402 KiB | 5 Q