3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '[ {name: "KFC", distance: 17, likes: 3}, {name: "Macdonalds", distance: 2, likes: 9}, {name: "Pizza Hut", distance: 9, likes: 12}, {name: "Burger King", distance: 14, likes: 17} ]'; $json = preg_replace('/(\w+)(?=:)/', '"$1"', $json); $restaurants = json_decode($json, true); function get_rating($restaurant, $weights) { $rating = 0; foreach ($weights as $factor => $weight) { $rating += $weight * $restaurant[$factor]; } return $rating; } $weights = array('distance' => -30, 'likes' => 10); usort($restaurants, function ($a, $b) use ($weights) { return get_rating($b, $weights) - get_rating($a, $weights); }); print_r($restaurants);
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.27, 8.3.0 - 8.3.15, 8.4.1 - 8.4.2
Array ( [0] => Array ( [name] => Macdonalds [distance] => 2 [likes] => 9 ) [1] => Array ( [name] => Pizza Hut [distance] => 9 [likes] => 12 ) [2] => Array ( [name] => Burger King [distance] => 14 [likes] => 17 ) [3] => Array ( [name] => KFC [distance] => 17 [likes] => 3 ) )

preferences:
140 ms | 409 KiB | 5 Q