I have some json filenames in an multidimensional array, created like this: $files[] = array("name" => $file, "type" => "json") that I want to sort ascending like this:
1.File
2.File
3.File
10.File
11.File
If I order them using php sort($files) function they will be ordered like this:
1.File
10.File
11.File
2.File
3.File
If however I use natsort($files) that quote: orders alphanumeric strings in the way a human being would (php.net), I get a total mess:
3.File
2.File
10.File
1.File
11.File
Is there some better approach to order an array using php functions? Or do I have to build a custom sorting function. The server is running PHP 7.0.
You use multidimensional array, as you can see PHP have an error (
Array to string conversion), you need to useusortfor that task like:Your fiddle edited
Reference:
As @u_mulder suggest
strnatcasecmpwill improve the code like:Reference: