Php 多优先级多数组排序

Php 多优先级多数组排序,php,arrays,sorting,Php,Arrays,Sorting,我有N个数组(例如3个数组): 我想根据(id)和(score)对这些(N)数组进行排序,但我想给所有数组中出现的重复id赋予优先级,然后给最大分数赋予第二优先级,结果将是(1)过滤的唯一数组,它根据这些排序规则保存唯一id 我尝试使用phpusort函数传递比较函数来完成此操作,但失败了。如果数据源来自mysql数据库,则可以通过简单的sql查询轻松检索此自定义排序 但若我们必须直接使用阵列,那个么这也应该起作用:(抱歉,编码太脏,移动性太差。) 如果数据源来自mysql数据库,则可以通过简单

我有N个数组(例如3个数组):

我想根据(id)和(score)对这些(N)数组进行排序,但我想给所有数组中出现的重复id赋予优先级,然后给最大分数赋予第二优先级,结果将是(1)过滤的唯一数组,它根据这些排序规则保存唯一id


我尝试使用php
usort
函数传递比较函数来完成此操作,但失败了。

如果数据源来自mysql数据库,则可以通过简单的sql查询轻松检索此自定义排序

但若我们必须直接使用阵列,那个么这也应该起作用:(抱歉,编码太脏,移动性太差。)


如果数据源来自mysql数据库,则可以通过简单的sql查询轻松检索此自定义排序

但若我们必须直接使用阵列,那个么这也应该起作用:(抱歉,编码太脏,移动性太差。)


这些信息来自数据库吗?是的,这些信息来自数据库,因为我建立了搜索索引表并想对结果进行排序。这些信息来自数据库吗?是的,这些信息来自数据库,因为我建立了搜索索引表,并希望对结果进行排序。精彩的,代码给出了所需的结果。谢谢。你能给我一个使用数据库的例子吗?精彩的,代码给出了所需的结果,谢谢。你能给我一个使用数据库的例子吗?
$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)

    );
$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 )