PHP algorithm-Sort file size (kb, MB, GB, Tb)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.