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);
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
array ( 44 => '0 MB', 24 => '4 MB', 4 => '10 MB', 12 => '20 MB', 32 => '50 MB', 0 => '100 MB', 16 => '250 MB', 28 => '500 MB', 8 => '1GB', 20 => '2GB', 36 => '5GB', 40 => '8GB', )

preferences:
79.82 ms | 407 KiB | 5 Q