Sort an array by value and store in a variable

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

I want to sort $array as asort , but the problem is that asort is a function and its product cannot 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 keep its original order.

+4
source share
3 answers

Do this to keep $array in 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

+7
source
  $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
source

Adjust it first and then assign it

 asort($array); $sorted_array = $array 
0
source

All Articles