doutu3352
2012-06-01 07:32
浏览 33
已采纳

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

I have a table/array. E.g. in Excel I can sort by column 1 asc, column 2 desc, column 3 asc, etc.

Can I do same in PHP? First with ["first_name"] ASC, then ["last_name"] DESC, ["player_id"] ASC and ["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"
    }
}

(The array is much longer and deeper in reality, this is just an example.)

Update:

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
}

Update 2: Never mind, I just needed to remove return 0;

图片转代码服务由CSDN问答提供 功能建议

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

我可以在PHP中执行相同操作吗? 首先使用 [“first_name”] ASC ,然后 [“last_name”] DESC [“player_id”] ASC [“ user_id“] DESC

  array(2){
 [0] =&gt;  array(6){
 [0] =&gt;  string(8)“John”,
 [“first_name”] =&gt;  string(8)“John”,
 [1] =&gt;  int(7)“44”,
 [“得分”] =&gt;  int(7)“44”,
 [2] =&gt;  string(2)“7”,
 [“player_id”] =&gt;  string(2)“7”,
 [3] =&gt;  string(2)“3”,
 [“user_id”] =&gt;  string(2)“3”
},
 [1] =&gt;  array(6){
 [0] =&gt;  string(5)“Sam”,
 [“first_name”] =&gt;  string(5)“Sam”,
 [1] =&gt;  int(7)“55”,
 [“得分”] =&gt;  int(7)“55”,
 [2] =&gt;  string(2)“1”,
 [“player_id”] =&gt;  string(2)“1”,
 [3] =&gt;  string(2)“6”,
 [“user_id”] =&gt;  string(2)“61”
} 
} 
   
 
 

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

更新:

  function byPlayerID($ player,$ compare){
 if($  player ['player_id']&gt; $ compare ['player_id'])
返回1;  //向上移动
 if if($ player ['player_id']&lt; $ compare ['player_id'])
返回-1;  //向下移动
其他
返回0;  //什么都不做
 
 if($ player ['score']&gt; $ compare ['score'])
返回1;  //向上移动
 if if($ player ['score']&lt; $ compare ['score'])
 return -1;  //向下移动
其他
返回0;  //什么都不做
} 
   
 
 

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dounaidu0204 2012-06-01 07:40
    已采纳

    Use usort().

    Example:

    $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!
    

    This does require PHP 5.3 though, below is a more backwards compatible version

    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");
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsajdgjadwqe3247382 2012-06-01 07:38

    Use the PHP function usort to compare values as you need. Extend your callback function to compare multiple values.

    评论
    解决 无用
    打赏 举报
  • dongxu4580 2012-06-01 07:47

    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");
    
    评论
    解决 无用
    打赏 举报
  • dongyan7988 2017-08-24 01:39

    For those who find this in the future, here's a final working version that fully answers the original question. This sorts by four different key values.

    $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);
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题