Having stayed at school, I went over PhP and looked at the previously written functions. I suddenly wanted to write the known file size and sort the data.
For example: 1.3G, 153 MB, 458kb, 998 m, 0.99g (the difference between MB and MB is not considered here, and generally does not involve the operation of MB, so MB, MB, and MB are regarded as mb. In fact, MB = 8 * MB)
The function is as follows:
<? PHP Function Sortsize ( $ Arr ){ $ Arr2 = $ Arr ; $ Arr3 = Array (); If (! Is_array ( $ Arr )){ Return False ;} Array_walk ( $ Arr , 'Getbytesize' ); Asort ( $ Arr ); Foreach ( $ Arr As $ Key =>$ Value ){ $ Arr3 [] = $ Arr2 [ $ Key ];} Return $ Arr3 ;} Function Getbytesize (& $ Size ){ $ Base = Array ( Array ('Kb', 'k '), Array ('M', 'M '), Array ('Gb', 'G '), Array ('Tb', 'T' )); $ Sum = 1 ; For ( $ I = 0; $ I <4; $ I ++ ){ If ( Stripos ($ Size , $ Base [ $ I ] [0]) | Stripos ( $ Size , $ Base [ $ I ] [1 ]) { $ Size = $ Sum * Str_ireplace ( $ Base [ $ I ], '',$ Size ) * 1024 ; Break ;} $ Sum * = 1024 ;}} $ Arr = Array ('23m', '1. 02g', '987mb', '45mb', '0. 98g' ); Print_r (Sortsize ( $ Arr )); ?>
Why are you upset,CodeSo many, changed, and refreshed?
<? PHP Function Sortsize ( $ , $ B ){ $ = Getbytesize ( $ ); $ B = Getbytesize ( $ B ); If ( $ =$ B ){ Return 0 ;} Return ( $ > $ B )? 1:-1 ;} Function Getbytesize ( $ Size ){ $ Base = Array ( Array ('Kb', 'k '),Array ('M', 'M '), Array ('Gb', 'G '), Array ('Tb', 'T' )); $ Sum = 1 ; For ( $ I = 0; $ I <4; $ I ++ ){ If ( Stripos ( $ Size , $ Base [ $ I ] [0]) | Stripos ( $ Size , $ Base [ $ I ] [1 ]) { $ Size = $ Sum * Str_ireplace ( $ Base [ $ I ], '', $ Size ) * 1024; Break ;} $ Sum * = 1024 ;} Return $ Size ;} $ Arr = Array ('23m', '1. 02g', '987mb', '45mb', '0. 98g' ); Usort ( $ Arr , 'Sortsize' ); Print_r ( $ Arr ); ?>
Comments