Kavya Shree Kavya Shree - 25 days ago 14
PHP Question

sort Folder names from directory Using PHP

Hii Everyone,

Here I get all files from folder and sort names using PHP.


<?php
$dir = "Car_Brands";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
sort($files);
print_r($files);
?>


It Gives Result in Order of

Array ( [0] => . [1] => .. [2] => ALTO [3] => BEAT [4] => CRUZE [5] => Civic mugen kit [6] => Civic type R kit [7] => Ertiga [8] => FIESTA [9] => FIGO [10] => I20 [11] => LANCER [12] => LINEA [13] => MANZA [14] => NEW I20 [15] => New Beat [16] => New Figo [17] => POLO [18] => SWIFT [19] => SX4 [20] => VENTO [21] => VERNA [22] => VISTA [23] => accord car modification [24] => civic customized kit [25] => hondacity [26] => hondacity 2nd generation [27] => hondacity 3rd generation [28] => octavia [29] => rapid )


But I want Order in ALTO and Accord should in first.If the words having space It moves last in alphabetical order Why is it so.How can I recover this Problem.Please anyone give me the solution.

Answer

They are moving to last not because of space. its because of sort case sensitive.

Try natcasesort

natcasesort($files);

I hope this will help you.

Comments