PHP的 - 排序多维数组 [英] PHP - Sorting multi-dimensional array

查看:116
本文介绍了PHP的 - 排序多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是法比奥和马西诺我是意大利人,所以我的英语可能是不完美的。

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


  • 由点(作为数字排序,按降序排列)

  • 然后进球(作为数字排序,按降序排列)

  • 然后按名称(如排序按升序字符串)。

例如,如果我有这个多维数组:

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');

如果你想专注于喜欢的名称和目标等标准,那么就更改数字数组索引到重新presents在每个子阵列的每个标准的数量。例如,分类名称也只是

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;
}

这篇关于PHP的 - 排序多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆