<?PHP $arr=array("1"=>"Carbon","2"=>"carbon","3"=>"Protein", "4"=>"fat",); asort($arr, SORT_STRING); foreach($arr as $key=>$val) echo "$key, $val; "; //1, Carbon; 3, Protein; 2, carbon; 4, fat; ?>
The sort can be modified by
• SORT_REGULAR: sort normally
• SORT_NUMERIC: sort numerically
• SORT_STRING: sort as strings
• SORT_LOCALE_STRING: sort as strings based on the current locale
• SORT_NATURAL: sort as strings using "natural ordering"
• SORT_FLAG_CASE: combined with SORT_STRING or SORT_NATURAL to sort case insensitively
Sort by string and case insensitive.
<?PHP $arr=array("1"=>"Carbon","2"=>"carbon","3"=>"Protein", "4"=>"fat",); asort($arr, SORT_STRING | SORT_FLAG_CASE); foreach($arr as $key=>$val) echo "$key, $val; "; //1, Carbon; 2, carbon; 4, fat; 3, Protein; ?>
<?PHP $arr=array("1"=>"Carbon","2"=>"carbon","3"=>"Protein", "4"=>"fat",); arsort($arr, SORT_STRING | SORT_FLAG_CASE); foreach($arr as $key=>$val) echo "$key, $val; "; //3, Protein; 4, fat; 2, carbon; 1, Carbon; ?>
Following code can sort all the files in a directory by file size.
<?PHP function allfiles($dir, $prefix = '') { $dir = rtrim($dir, '\\/'); $result = array(); foreach (scandir($dir) as $f) { if ($f !== '.' and $f !== '..') { if (is_dir("$dir\\$f")) { $result = array_merge($result, allfiles("$dir\\$f", "$prefix$f\\")); } else { $result[] = $prefix.$f; } } } return $result; } $dir = "used_files/"; $files = allfiles($dir); $arrsizes = array(); foreach ($files as $file) { $arrsizes[$file] = filesize($dir.$file); } arsort($arrsizes, SORT_NUMERIC); //from largest size to smallest size //asort($arrsizes, , SORT_NUMERIC); //reverse $files = array_keys($arrsizes); ?>