1
occurrence of A----5 times
occurrence of B----7 times
occurrence of C----6 times
($total['A']=5;
 $total['B']=7;
 $total['C']=6;)

次のように、発生に基づいてそれぞれを評価する必要があります。

$rating['A']=3;
$rating['B']=1;//most occurrence will get rate 1.
$rating['C']=2;
4

2 に答える 2

2
asort(&$total);
$rating = 1;
foreach (array_reverse($total) as $key => $val)
    $total[$key] = $rating++;
于 2013-01-24T15:40:21.257 に答える
0

You could also try using the array functions like sorting the values first and get keys of the sorted array and then create another array with the extracted keys as values. Now you get the same array with their rankings, except that you have '0' rank first as arrays start with an index 0.

May be you could pad an additional value with '0' an then finally remove it.

Something like this,

$total['A']=5;
$total['B']=7;
$total['C']=6;

$total[0]=0;
asort($total);
$total = array_slice(array_flip(array_keys($total)),1);

Or if you don't like to pad an extra value, you can try it this way. Create an array with the keys and the count of the sorted array.

asort($total);
$total = array_combine(array_keys($total),range(1, count($total)));

This might be a quick and easy way of doing. Hope this helps.

于 2013-01-24T16:15:47.567 に答える