<?php const UNIT_FACTOR = [ 'g' => 1, 'Kg' => 1000, ]; $weights = ['1 Kg','300 g','1.5 Kg','20 g','5 Kg']; $grams = []; foreach ($weights as $weight) { sscanf($weight, '%f%s', $amount, $unit); $grams[] = $amount * UNIT_FACTOR[$unit]; } array_multisort($grams, $weights); var_export($weights);
You have javascript disabled. You will not be able to edit any code.