English 中文(简体)
php 区分多阵列,具有多个优先事项
原标题:php sorting multi arrays with multi priority

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)

    );

我想根据(id)和(核心)来分类这些(N)阵列,但我要优先注意所有阵列中的重复发生,然后是最高分数的第二大优先事项,其结果将是(1) 过滤独特的阵列,根据这些规则,这些阵列具有独一无二之处。

我试图利用php<代码>usort功能通过比较功能,但我没有做这项工作。

最佳回答

如果数据来源来自我的ql数据库,这种习俗可以很容易地用简单的 s子检索。

但是,如果我们必须与阵列直接合作,那么这也应当做到: (关于 d脏编码和流动性差的担忧)

$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 )
问题回答

暂无回答




相关问题
How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

Can I Nest OrderBy in .NET?

This doesn t seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m) m.Sent).First.Sent) For Each d As Discussion In x ....

sorting elements javascript

I m looking for a way to sort my elements, but it isn t as easy as it sounds. Please let me explain My elements are grouped per 6 elements (thumbnails), each x represents a thumbnail However all ...

热门标签