php 对数组进行排序并保留键的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15762256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:55:09  来源:igfitidea点击:

Sort array and keep values of keys

phparrayssorting

提问by acrmuui

I have an array that looks like this:

我有一个看起来像这样的数组:

Array
(
    [team1] => Array
        (
            [points] => 10
            [players] => Array
                (
                     ...
                )
        )

    [team2] => Array
        (
            [points] => 23
            [players] => Array
                (
                     ...
                )
        )

    ... many more teams
)

and I would like to sort the teams by the number of points each team has. I have tried this:

我想按照每支球队的得分对球队进行排序。我试过这个:

function sort_by_points($a,$b)
{
    if ($a['points']==$b['points']) return 0;
        return ($a['points']<$b['points'])?1:-1;
}

usort($this->wordswithdata, "sortbycount");

But that approach overrides the keys containing the teamnames and returns:

但是这种方法会覆盖包含团队名称的键并返回:

Array
(
    [0] => Array
        (
            [points] => 23
            [players] => Array
                (
                     ...
                )
        )

    [1] => Array
        (
            [points] => 10
            [players] => Array
                (
                     ...
                )
        )

    etc...
)

Is there any way to sort the array without overwriting the teamnames as the array keys?

有没有办法在不覆盖团队名称作为数组键的情况下对数组进行排序?

采纳答案by complex857

Use the uasortfunction, that should keep the key => value associations intact.

使用uasort函数,应该保持键 => 值关联完整。

(side note: you can do return $a['points'] - $b['points']instead of the ifs)

(旁注:你可以return $a['points'] - $b['points']代替 ifs)

回答by Shoe

You can use uasort:

您可以使用uasort

uasort($array, function($a, $b) {
    return $a['points'] - $b['points'];
});

This function sorts an array such that array indices maintain their correlationwith the array elements they are associated with, using a user-defined comparison function.

此函数使用用户定义的比较函数对数组进行排序,以便数组索引保持与其关联的数组元素的相关性。

回答by Aylian Craspa

U can sort an Associative array by its value like this

你可以像这样按其值对关联数组进行排序

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

function Ascending($a, $b) {   
    if ($a == $b) {        
        return 0;
    }   
        return ($a < $b) ? -1 : 1; 
}  

function Descending($a, $b) {   
    if ($a == $b) {        
        return 0;
    }   
        return ($a > $b) ? -1 : 1; 
}  


echo "Ascending order" ;
uasort($age,"Ascending");
print_r($age);


echo "</br>Descending order" ;
uasort($age,"Descending");
print_r($age);

回答by Satyendra Singh

its working fine i giving example
that array need sort
$sumarray=array [▼
  484 => 54.7875
  486 => 53.5375
  487 => 52.9125
  488 => 52.2875
  493 => 54.7875
]

$original=$sumarray;

 $svalue=array_values($sumarray);
  rsort($svalue);
  $sorted=array();
  foreach ($svalue as $key => $value) {
   $kk=array_search($value,$sumarray);
  $sorted[$kk]=$value;
  unset($sumarray[$kk]);

    }
  print_r($original);
    print_r($svalue);
  print_r($sorted);
             //out put
  array:5 [▼
  484 => 54.7875
  486 => 53.5375
  487 => 52.9125
  488 => 52.2875
  493 => 54.7875
]
array:5 [▼
  0 => 54.7875
  1 => 54.7875
  2 => 53.5375
  3 => 52.9125
  4 => 52.2875
]
array:5 [▼
  484 => 54.7875
  493 => 54.7875
  486 => 53.5375
  487 => 52.9125
  488 => 52.2875
]

回答by Praveen kalal

Try this code hope it will work.

试试这个代码希望它会起作用。

function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}

aasort($your_array,"points");