1

有没有办法重新排序数组中的值?例如我有

 Array (
    "one" => 0,
     "tow" => 0,
     "three" => 0,
     "four" => 8,
     "apple" => 4,
     "pink" => 3,
   );

并将其转换为

 Array (
    "one" => 0,
     "tow" => 1,
     "three" => 2,
     "pink" => 3,
     "apple" => 4,
     "four" => 5,
   );

编辑:

请注意,“4”的值更大,应该更改为 5,“apple”和“pink”不应该更改

4

3 回答 3

6

怎么这么简单...

$source  = array('one' => 0, 'tow' => 0, 'three' => 0, 'four' => 8, 'apple' => 4, 'pink' => 3);
asort($source);
$result  = array_flip(array_keys($source));

说明:array_keys将原始数组的所有键收集为另一个索引数组,并将array_flip这些索引转换为值。)

于 2012-12-19T11:57:07.337 回答
1
$i = 0;
foreach( $array as $key => $value )
{
    $array[$key] = $i;
    $i++
}

应该这样做:-)

于 2012-12-19T11:56:47.977 回答
0

您可能正在寻找PHP asort()

于 2012-12-19T11:57:54.033 回答