简体   繁体   English

PHP - 排序多维数组

[英]PHP - Sorting multi-dimensional array

i'm Fabio Masino and i'm italian, so my English may be not perfect. 我是法比奥马西诺,我是意大利人,所以我的英语可能并不完美。

I would like to realize a method for sorting football groups 我想实现一种分类足球组的方法

  • by points (sorted as numbers, in descending order), 按点(按数字排序,按降序排列),
  • then by goals scored (sorted as numbers, in descending order), 然后根据得分(按数字排序,按降序排列),
  • then by name (sorted as strings in ascending order). 然后按名称(按升序排序为字符串)。

For example, if i have this multi-dimensional array: 例如,如果我有这个多维数组:

$group=array(
       array("Juve", 15, 45), // the values are name, points and goals scored
       array("Inter", 21, 40),
       array("Milan", 15, 50)
      );

I would like to have this result: 我想得到这样的结果:

 $group=array(
           array("Inter", 21, 40),
           array("Milan", 15, 50),
           array("Juve", 15, 45)
          );

Thank you in advance and best regards. 提前谢谢你和最好的问候。

Points is the second element of each subarray, right? 积分是每个子阵列的第二个元素,对吧? If so, then do this 如果是这样,那么这样做

function CustomSort($a, $b)
{
    return $a[1] < $b[1] ? -1 : 1;
}

usort($group, 'CustomSort');

If you want to focus on other criteria like names and goals, then just change the numeric array index to the number that represents each criteria in each subarray. 如果您想关注名称和目标等其他条件,则只需将数值数组索引更改为代表每个子数组中每个条件的数字。 For example, sorting names would just be 例如,排序名称就是

function NameSort($a, $b)
{
    return $a[0] > $b[0] ? -1 : 1;
}
$group = array(
    array("Juve", 15, 45), // the values are name, points and goals scored
    array("Inter", 21, 40),
    array("Milan", 15, 50)
);

usort(
    $group,
    function($a, $b) {
        if ($a[1] == $b[1]) {
            if ($a[2] == $b[2]) {
                return ($a[0] < $b[0]) ? -1 : 1;  // by team name (ascending)
            }
            return ($a[2] < $b[2]) ? 1 : -1;  // by goals scored (descending)
        }
        return ($a[1] < $b[1]) ? 1 : -1;  // by points (descending)
    }
);

var_dump($group);
$group=array(
       array("Juve", 15, 45), // the values are name, points and goals scored
       array("Inter", 21, 40),
       array("Milan", 15, 50)
      );

// Obtain a list of columns
foreach ($group as $key => $row) {
    $team[$key]  = $row[0];
    $point[$key] = $row[1];
    $goal[$key] = $row[2];
}

array_multisort($point, SORT_DESC, $goal, SORT_DESC, $group);

echo "<pre>";
print_r($group);

phpfiddle phpfiddle

sorting function : 排序功能:

Use my custom function to achieve your solution it is working 使用我的自定义功能来实现它正在运行的解决方案

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

multisort($multiarr,"order");

Hope this will sure help you. 希望这一定会对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM