5
$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.

2
  • Their is no need to store the result of asort because it is sorted inside $array and now you can directly access sorted array using $array. Commented Mar 23, 2013 at 5:13
  • Yes, but I also lose my original order. Commented Mar 23, 2013 at 5:16

3 Answers 3

9

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

Sign up to request clarification or add additional context in comments.

Comments

2
 $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 :(

2 Comments

I should've mentioned that I want $array to keep its original order.
I still think there is a better way to do this.
0

Sort it first and then assign it

asort($array);
$sorted_array = $array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.