Sort array by value and store in variable

浪子不回头ぞ 提交于 2020-01-11 08:59:11

问题


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


回答1:


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




回答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 :(



回答3:


Sort it first and then assign it

asort($array);
$sorted_array = $array


来源:https://stackoverflow.com/questions/15583611/sort-array-by-value-and-store-in-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!