I have this array:
Array (
[0] => ../files/flv/1
[1] => ../files/flv/10
[2] => ../files/flv/2
[3] => ../files/flv/3
[4] => ../files/flv/4
[5] => ../files/flv/5
[6] => ../files/flv/6
[7] => ../files/flv/7
[8] => ../files/flv/8
[9] => ../files/flv/9
)
I need to sort it this way:
Array (
[0] => ../files/flv/1
[1] => ../files/flv/2
[2] => ../files/flv/3
[3] => ../files/flv/4
[4] => ../files/flv/5
[5] => ../files/flv/6
[6] => ../files/flv/7
[7] => ../files/flv/8
[8] => ../files/flv/9
[9] => ../files/flv/10
)
I tried to use sort($array,SORT_NUMERIC);
, but no luck because of this prefix ../files/flv/
I know only this solution: $array2 = array_map('basename', $array);
and then sort($array2,SORT_NUMERIC);
Is there any other solutions not so complex?
Use SORT_NATURAL
instead of SORT_NUMERIC
(requires PHP 5.4.0 or latest):
sort($array, SORT_NATURAL);
EDIT: I used this code to test it:
$array = array(
'../files/flv/1',
'../files/flv/10',
'../files/flv/2'
);
sort($array, SORT_NATURAL);
print_r($array);
It outputs:
Array
(
[0] => ../files/flv/1
[1] => ../files/flv/2
[2] => ../files/flv/10
)
EDIT 2: Alternatively you can use the natsort()
function, it works on older versions too:
natsort($array);