在PHP中按多个键对数组进行排序

时间:2012-06-01 07:32:14

标签: php arrays sorting

我有一个表/数组。例如。在Excel中,我可以按列1 asc,第2列desc,第3列asc等进行排序。

我可以在PHP中执行相同的操作吗?首先是["first_name"] ASC,然后是["last_name"] DESC["player_id"] ASC["user_id"] DESC

array(2) {
    [0]=> array(6) {
        [0]=> string(8) "John",
        ["first_name"]=> string(8) "John",
        [1]=> int(7) "44",
        ["score"]=> int(7) "44",
        [2]=> string(2) "7",
        ["player_id"]=> string(2) "7",
        [3]=> string(2) "3",
        ["user_id"]=> string(2) "3"
    },
    [1]=> array(6) {
        [0]=> string(5) "Sam",
        ["first_name"]=> string(5) "Sam",
        [1]=> int(7) "55",
        ["score"]=> int(7) "55",
        [2]=> string(2) "1",
        ["player_id"]=> string(2) "1",
        [3]=> string(2) "6",
        ["user_id"]=> string(2) "61"
    }
}

(实际上阵列更长更深,这只是一个例子。)

更新:

function byPlayerID($player, $compare) {
    if($player['player_id'] > $compare['player_id'])
        return 1; // move up
    else if($player['player_id'] < $compare['player_id'])
        return -1; // move down
    else
        return 0; // do nothing

    if($player['score'] > $compare['score'])
        return 1; // move up
    else if($player['score'] < $compare['score'])
        return -1; // move down
    else
        return 0; // do nothing
}

更新2:没关系,我只需要删除return 0;

4 个答案:

答案 0 :(得分:4)

array_multisort是您正在寻找的功能。

这是使用array_multisort

编写的示例函数

https://gist.github.com/1220785

使用

   $sorted_arraty = sort_array_multidim($array,"first_name ASC, last_name DESC, player_id ASC, user_id DESC");

答案 1 :(得分:1)

使用usort()。

示例:

$byPlayerID = function($player, $compare) {
  if($player['player_id'] > $compare['player_id'])
    return 1; // move up
  else if($player['player_id'] < $compare['player_id'])
    return -1; // move down
  else
    return 0; // do nothing
};

usort($players, $byPlayerID);
// now $players is sorted!

这确实需要PHP 5.3,下面是一个更向后兼容的版本

function byPlayerID($player, $compare) {
  if($player['player_id'] > $compare['player_id'])
    return 1; // move up
  else if($player['player_id'] < $compare['player_id'])
    return -1; // move down
  else
    return 0; // do nothing
}

usort($players, "byPlayerID");

答案 2 :(得分:0)

使用PHP函数usort根据需要比较值。扩展回调函数以比较多个值。

答案 3 :(得分:0)

对于那些将来发现这一点的人来说,这是一个完整回答原始问题的最终工作版本。这按四个不同的键值排序。

$byPlayerID = function($a, $b) {

  // Sort by first
  // > to return 1, < to return -1 = ASC order
  if($a['first_name'] > $b['first_name']) return 1; // move up
  else if($a['first_name'] < $b['first_name']) return -1; // move down

  // Sort by second
  // < to return 1, > to return -1 = DESC order
  if($a['last_name'] < $b['last_name']) return 1; // move up
  else if($a['last_name'] > $b['last_name']) return -1; // move down

  // Sort by third
  // > to return 1, < to return -1 = ASC order
  if($a['player_id'] > $b['player_id']) return 1; // move up
  else if($a['player_id'] < $b['player_id']) return -1; // move down

  // Sort by fourth
  // < to return 1, > to return -1 = DESC order
  if($a['user_id'] < $b['user_id']) return 1; // move up
  else if($a['user_id'] > $b['user_id']) return -1; // move down

  else return 0; // do nothing

};

usort($stats, $byPlayerID);