user2930485
user2930485

Reputation:

Sort Array and Store Indexes in a seperate array

I'd like to sort this array and store the indexes into another array while keeping this original one in the order it is now.

Array ( [0] => 2000 
        [1] => 2015 
        [2] => 2004 
        [3] => 1990 
        [4] => 1995 
        [5] => 1965 
        [6] => 1990 ) 

So the new one should look something like this:

Array ( [0] => 5 
        [1] => 3 
        [2] => 6 
        [3] => 4 
        [4] => 0 
        [5] => 2 
        [6] => 1 ) 

Upvotes: 2

Views: 43

Answers (1)

rcpinto
rcpinto

Reputation: 4308

Create a copy of your array; Use asort to preserve indexes; use array_keys to get your array containing the indexes:

$arr = array(2000,2015,2004,1990,1995,1965,1990);
$arr2 = $arr;
asort($arr2);
$indexes = array_keys($arr2);

Upvotes: 2

Related Questions