Order a PHP array based on contents
I have an array in PHP which looks like this:
Array
(
[2] => post4.post
[3] => post7.post
[4] => post5.post
[5] => post3.post
[6] => post6.post
[7] => post1.post
[8] => post2.post
)
How could I arrange it so it could look like this?
Array
(
[0] => post7.post
[1] => post6.post
[2] => post5.post
[3] => post4.post
[4] => post3.post
[5] => post2.post
[6] => post1.post
)
The array has a list of the files contained in a folder. In my local server looks like the second example but on a standard server looks like the fir开发者_开发知识库st one.
Thanks in advance :D
You are looking for rsort
.
PHP has great documentation. I suggest you have a look at the array functions and the Sorting Arrays article (which can be easily find via Google ;))
rsort($array)
will sort an array in reverse/descending order (from high to low).
For future reference use this page (http://php.net/manual/en/array.sorting.php) to identify the most suited array sorting method for your needs.
You don't want to use rsort
because it will reassign array indexes. Instead use arsort().
精彩评论