3v4l.org

run code in 500+ PHP versions simultaneously
<?php const UNIT_FACTOR = [ 'g' => 1, 'Kg' => 1000, ]; $weights = ['1 Kg','300 g','1.5 Kg','20 g','5 Kg']; foreach ($weights as $weight) { sscanf($weight, '%f %s', $amount, $unit); $grams[] = $amount * UNIT_FACTOR[$unit]; } array_multisort($grams, $weights); var_export($weights);

preferences:
72.76 ms | 1266 KiB | 5 Q