pmerino
pmerino

Reputation: 6110

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 first one.

Thanks in advance :D

Upvotes: 0

Views: 52

Answers (3)

GordyD
GordyD

Reputation: 5103

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.

Upvotes: 0

user188654
user188654

Reputation:

You don't want to use rsort because it will reassign array indexes. Instead use arsort().

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816364

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 ;))

Upvotes: 4

Related Questions