How do I sort the following data in the order I want it?
The array:
array(4)
{
[21]=> string(7) "2-2.pdf"
[22]=> string(7) "2-3.pdf"
[23]=> string(7) "2-4.pdf"
[24]=> string(5) "2.pdf"
}
I want this sort:
2.pdf
2-2.pdf
2-3.pdf
2-4.pdf
You can do like this:
<?php
$check_array = array('2-2.pdf','2.pdf','2-3.pdf','2-4.pdf');
function cmp($a, $b)
{
$a = preg_replace('/-/','',$a);
$b = preg_replace('/-/','',$b);
return strcmp($a, $b);
}
usort($check_array, "cmp");
echo "<pre/>"; print_r($check_array);
?>
And the result is:
<?php Array ( [0] => 2.pdf [1] => 2-2.pdf [2] => 2-3.pdf [3] => 2-4.pdf ) ?>
usort()