3v4l.org

run code in 300+ PHP versions simultaneously
<?php const UNIT_FACTOR = [ 'B' => 1, 'KB' => 1024, 'MB' => 1048576, 'GB' => 1073741824, ]; $array = [ 0 => "100 MB", 4 => "10 MB", 8 => "1GB", 12 => "20 MB", 16 => "250 MB", 20 => "2GB", 24 => "4 MB", 28 => "500 MB", 32 => "50 MB", 36 => "5GB", 40 => "8GB", 44 => "0 MB" ]; $bytes = []; foreach ($array as $string) { sscanf($string, '%f%s', $amount, $unit); $bytes[$string] = $amount * UNIT_FACTOR[$unit]; } uasort($array, fn($a, $b) => $bytes[$a] <=> $bytes[$b]); var_export($array);

preferences:
27.82 ms | 406 KiB | 5 Q