我有像PHP一样的数组
$somevar = array(1, 5, 2, 7, 17, 2, 13);
我想对数组进行排序,但不要移动值,所以我得到另一个数组,其中包含索引/键的顺序。
所以我的新阵列就像是
{0, 2, 5, 1, 3, 6, 4}
这是键的顺序
答案 0 :(得分:3)
这应该适合你:
<?php
$somevar = array(1, 5, 2, 7, 17, 2, 13);
$newArray = array_values($somevar);
asort($newArray);
$newArray = array_keys($newArray);
print_r($newArray);
?>
输出:
Array ( [0] => 0 [1] => 2 [2] => 5 [3] => 1 [4] => 3 [5] => 6 [6] => 4 )
答案 1 :(得分:2)
试试这个..
$somevar = array(1, 5, 2, 7, 17, 2, 13);
print_r($somevar);
//your array
Array ( [0] => 1 [1] => 5 [2] => 2 [3] => 7 [4] => 17 [5] => 2 [6] => 13 )
asort($somevar);
print_r($somevar);
//After sort
Array ( [0] => 1 [2] => 2 [5] => 2 [1] => 5 [3] => 7 [6] => 13 [4] => 17 )
print_r(array_keys($somevar));
//array key sorted array
Array ( [0] => 0 [1] => 2 [2] => 5 [3] => 1 [4] => 3 [5] => 6 [6] => 4 )