UserIsCorrupt
UserIsCorrupt

Reputation: 5025

Sort array by value and store in variable

$array = array(5,4,6,8,5,3,4,6,1);

I want to sort $array like asort does, but the problem is that asort is a function and its product can't be stored in a variable.

How can I do something like this?:

$array = array(5,4,6,8,5,3,4,6,1);
$sorted_array = asort($array);

Edit: I also want $array to keep its original order.

Upvotes: 5

Views: 5072

Answers (3)

user2189593
user2189593

Reputation:

 $orignal_array = array(5,4,6,8,5,3,4,6,1);
 $copied_array = $orignal_array;

 asort($copied_array);
 $sorted_array = $copied_array;

 not the most efficient way to do it though :(

Upvotes: 2

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

Do this for maintaining $array in its original order

$array = array(5,4,6,8,5,3,4,6,1);
$sorted_array = $array;
asort($sorted_array);

Output

http://codepad.viper-7.com/8E78Fo

Upvotes: 9

alwaysLearn
alwaysLearn

Reputation: 6950

Sort it first and then assign it

asort($array);
$sorted_array = $array

Upvotes: 0

Related Questions