douzhenqun1271 2012-05-03 20:39
浏览 76
已采纳

php排序具有多个优先级的多个数组

I have N number of arrays (eg. 3 arrays):

$arr1 = array(0 => array('id' => 34, 'score' => 440),
          1 => array('id' => 32, 'score' => 140),
          2 => array('id' => 22, 'score' => 121),
          3 => array('id' => 99, 'score' => 532)
    );

$arr2 = array(0 => array('id' => 32, 'score' => 213),
          1 => array('id' => 34, 'score' => 354),
          2 => array('id' => 22, 'score' => 674)
    );

$arr3 = array(0 => array('id' => 34, 'score' => 10),
          1 => array('id' => 22, 'score' => 449),
          2 => array('id' => 99, 'score' => 586),
          3 => array('id' => 32, 'score' => 113),
          4 => array('id' => 16, 'score' => 777)

    );

I want to sort these (N) arrays depending on (id) and (score) but i want to give the priority to the repetition id occurrence in all arrays and then the second priority to the max score, and the result will be (1) filtered unique array which holds unique ids according to these rules of sort.

I tried to do this using php usort function to pass comparison function but i failed to do the job.

  • 写回答

1条回答 默认 最新

  • dongque3797 2012-05-03 22:03
    关注

    If the source of the data comes from a mysql database, this custom sort can be easily retrieved with a simple sql query.

    But if we must work with the arrays directly then this should work too: (Sorry for dirty coding and poor mobility.)

    $all_arrays = array_merge( $arr1, $arr2, $arr3 ); // merge all arrays into one
    $items = $ascores = $scores = $occurs = $sorted_ids = array();
    foreach( $all_arrays as $elem ) {
        if(isset($occurs[ $elem['id'] ])) { $occurs[ $elem['id'] ]++; } else { $occurs[ $elem['id'] ] = 1; }
        if( ! isset($ascores[ $elem['id'] ]) || $elem['score'] > max( $ascores[ $elem['id'] ] ) ) { 
            $ascores[ $elem['id'] ][] = $elem['score']; 
            $scores[ $elem['id'] ] = $elem['score']; 
        }
        $items[ $elem['id'] ] = array( 'id'=>$elem['id'], 'maxs'=>$scores[ $elem['id'] ], 'occs'=>$occurs[ $elem['id'] ] );
    }
    
    array_multisort( $occurs, SORT_DESC, $scores, SORT_DESC, $items);
    /// print_r( $items ); // $items holds unique sorted data. outputs: Array ( [0] => Array ( [id] => 22 [maxs] => 674 [occs] => 3 ) [1] => Array ( [id] => 34 [maxs] => 440 [occs] => 3 ) [2] => Array ( [id] => 32 [maxs] => 213 [occs] => 3 ) [3] => Array ( [id] => 99 [maxs] => 586 [occs] => 2 ) [4] => Array ( [id] => 16 [maxs] => 777 [occs] => 1 ) )
    
    foreach( $items as $item ) $sorted_ids[] = $item['id'];
    /// print_r( $sorted_ids ); // $sorted_ids holds your desired ids list. outputs: Array ( [0] => 22 [1] => 34 [2] => 32 [3] => 99 [4] => 16 )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏