scnadir sort order asc put uppercase letters first

醉酒当歌 提交于 2019-12-13 07:59:56

问题


So I am trying to sort a list of folders and files and display them alphabetical the problem appears that if someone created a folder that starts with a captial letter that folder appears first for example if I had the following folders

Array
(
    [0] => .
    [1] => ..
    [2] => _base
    [3] => template
    [4] => Website
)

I would expect when using scandir (scandir($directory, SCANDIR_SORT_ASCENDING)) to see the folders listed out as above but instead that get listed out as

Array
(
    [0] => .
    [1] => ..
    [2] => Website
    [3] => _base
    [4] => template
)

How would I be able to get this list sorted the correct way so that it wouldn't be case sensitive.


回答1:


This should work for you:

natcasesort($array);

It sort's an array natural ignore case

See: http://php.net/manual/en/function.natcasesort.php




回答2:


Maybe You should just use sort() function?

Example:

$x = array('.', '..', '22331', 'djsnaso', 'Aijndod', 'Wwwwww');

sort($x);

var_dump($x);

Return value:

array (size=6)
  0 => string '.' (length=1)
  1 => string '..' (length=2)
  2 => string '22331' (length=5)
  3 => string 'Aijndod' (length=7)
  4 => string 'Wwwwww' (length=6)
  5 => string 'djsnaso' (length=7)


来源:https://stackoverflow.com/questions/27173295/scnadir-sort-order-asc-put-uppercase-letters-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!