【问题标题】:ranking/position on multidimensional array多维数组上的排名/位置
【发布时间】:2015-02-16 14:45:23
【问题描述】:

我有一个这样的数组:

    Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [user_id] => 13162
                    [selling_points] => 110.2
                    [total_points] => 189.6
                    [contest_name] => Gold
                [position_selling] => 0
                [position_final] => 0
            )

        [1] => Array
            (
                [user_id] => 16712
                [selling_points] => 80.4
                [total_points] => 90.3
                [contest_name] => Gold
                [position_selling] => 0
                [position_final] => 0
            )
    )

[2] => Array
    (
        [0] => Array
            (
                [user_id] => 24613
                [selling_points] => 1400.72
                [total_points] => 1978.29
                [contest_name] => Silver
                [position_selling] => 0
                [position_final] => 0
            )
        [1] => Array
            (
                [user_id] => 41317
                [selling_points] => 775.33
                [total_points] => 847
                [contest_name] => Silver
                [position_selling] => 0
                [position_final] => 0
            )
        [2] => Array
            (
                [user_id] => 41045
                [selling_points] => 655.03
                [total_points] => 1065
                [contest_name] => Silver
                [position_selling] => 0
                [position_final] => 0
            )
        )
)

我想通过 sell_points (position_ sell) 和 total_points (position_final) 的值来分配 2 种仓位。

结果应该是这样的:

Array
(
    [1] => Array
        (
            [0] => Array
                (
                [user_id] => 13162
                [selling_points] => 110.2
                [total_points] => 189.6
                [contest_name] => Gold
                [position_selling] => 1
                [position_final] => 1
            )

        [1] => Array
            (
                [user_id] => 16712
                [selling_points] => 80.4
                [total_points] => 90.3
                [contest_name] => Gold
                [position_selling] => 2
                [position_final] => 2
            )
    )

[2] => Array
    (
        [0] => Array
            (
                [user_id] => 24613
                [selling_points] => 1400.72
                [total_points] => 1978.29
                [contest_name] => Silver
                [position_selling] => 1
                [position_final] => 1
            )

        [2] => Array
            (
                [user_id] => 41045
                [selling_points] => 655.03
                [total_points] => 1065
                [contest_name] => Silver
                [position_selling] => 3
                [position_final] => 2
            )

        [3] => Array
            (
                [user_id] => 41317
                [selling_points] => 775.33
                [total_points] => 847
                [contest_name] => Silver
                [position_selling] => 2
                [position_final] => 3
            )
        )
)

我曾尝试使用诸如 usort 和 array_multisort 之类的 php 函数,但没有成功...有人可以帮助我吗?

【问题讨论】:

    标签: php arrays sorting multidimensional-array ranking


    【解决方案1】:
    <?php
    $data = getData();
    $s1 = array();
    foreach( $data as &$v ) {
        $s1[] = &$v;
    }
    $s2 = $s1;
    usort( $s1, compareBy('selling_points'));
    usort( $s2, compareBy('total_points'));
    
    markPosition($s1, 'position_selling');
    markPosition($s2, 'position_final');
    
    echo '$data=', var_export($data); echo ";\r\n\r\n";
    
    
    function markPosition(array $arr, $key) {
        foreach( $arr as $v=>&$k ) {
            $k[$key] = $v+1;
        }
    }
    
    function compareBy($key) {
        return function($a,$b) use($key) {
            return $b[$key] - $a[$key];
        };
    }
    
    
    function getData() {
        return [
            [
                'user_id' => 1,
                'selling_points' => 1,
                'total_points' => 1,
            ],
            [
                'user_id' => 2,
                'selling_points' => 4,
                'total_points' => 4,
            ],
            [
                'user_id' => 3,
                'selling_points' => 3,
                'total_points' => 5,
            ],
        ];
    }
    

    现在没有时间解释它(如果你需要解释,明天提醒我)。和/或等待更好的解决方案.... ;-)
    我遗漏了您的数组的一个嵌套级别,但这应该是微不足道的。

    http://docs.php.net/references
    并且不要在没有 markPosition() 函数的情况下尝试它,否则您可能会因为 foreach 循环中的引用而导致意外行为......

    【讨论】:

      【解决方案2】:

      正如我所见,@VlkerK 已经给了你几乎和我准备的一样的答案。

      $arr = [
               [
                 ['user_id' => 13162,
                  'selling_points' => 110.2,
                  'total_points' => 189.6,
                  'contest_name' => 'Gold',
                  'position_selling' => 0,
                  'position_final' => 0
                 ],
                 ['user_id' => 16712,
                  'selling_points' => 80.4,
                  'total_points' => 90.3,
                  'contest_name' => 'Gold',
                  'position_selling' => 0,
                  'position_final' => 0
                 ]
               ],
               [
                 ['user_id' => 24613,
                  'selling_points' => 1400.72,
                  'total_points' => 1978.29,
                  'contest_name' => 'Silver',
                  'position_selling' => 0,
                  'position_final' => 0
                 ],
                 ['user_id' => 41317,
                  'selling_points' => 775.33,
                  'total_points' => 847,
                  'contest_name' => 'Silver',
                  'position_selling' => 0,
                  'position_final' => 0
                 ],
                 ['user_id' => 41045,
                  'selling_points' => 655.03,
                  'total_points' => 1065,
                  'contest_name' => 'Silver',
                  'position_selling' => 0,
                  'position_final' => 0
                 ]
               ]
              ];
      
      $newArr = array();
      
      function cmp($a, $b)
      {
          if ($a['selling_points'] == $b['selling_points']) {
              return 0;
          }
          return ($a['selling_points'] < $b['selling_points']) ? 1 : -1;
      }
      
      function cmpTotal($a, $b)
      {
          if ($a['total_points'] == $b['total_points']) {
              return 0;
          }
          return ($a['total_points'] < $b['total_points']) ? 1 : -1;
      }
      
      foreach($arr as $contest) {
      
          usort($contest,'cmp');
          $byTotal = $contest;
          usort($byTotal,'cmpTotal');
          $iTotal=1;
          foreach($byTotal as &$user) {
              $i = array_search($user,$contest);
              $user['position_selling'] = $i+1;
              $user['position_final'] = $iTotal++;
          }
          $newArr[] = $byTotal;
      }
      
      print_r($newArr);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-27
        • 1970-01-01
        • 2017-10-22
        • 2018-09-20
        • 1970-01-01
        • 1970-01-01
        • 2012-10-16
        • 1970-01-01
        相关资源
        最近更新 更多