我有一组值(键不重要):
$Array = array("File01","File02","File00","_File03");
我想按值对其进行排序,以匹配我的Windows文件系统,例如:
Array ( [3] => _File03 [2] => File00 [0] => File01 [1] => File02 )
我试过了asort($Array)
,但这给了我:
Array ( [2] => File00 [0] => File01 [1] => File02 [3] => _File03 )
是否可以先使用下划线进行排序?
答案 0 :(得分:1)
试试这个..它的工作。
<?php
$array = array("File01","File02","File00","_File03");
function sortUnderscoreToFront($a, $b) {
if (substr($a, 0, 1) == '_' || substr($b, 0, 1) == '_') {
return ((substr($a,0,1)=='_')?-1:1);
}
return strcmp(strval($a), strval($b));
}
usort($array, 'sortUnderscoreToFront');
echo "<pre>";
print_r($array)."</pre>";
?>