0

我有一个表/数组。例如,在 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

4 回答 4

4

array_multisort is the function you are looking for.

And here is an example function written using array_multisort

https://gist.github.com/1220785

using

   $sorted_arraty = sort_array_multidim($array,"first_name ASC, last_name DESC, player_id ASC, user_id DESC");
于 2012-06-01T07:47:00.190 回答
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");
于 2012-06-01T07:40:11.647 回答
0

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

于 2012-06-01T07:38:27.933 回答
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);
于 2017-08-24T01:39:46.770 回答