我有一个包含这些得分值的数组:
$r = Array
(
[C] => 11.00.00
[B] => 11.00.00
[G] => 11.00.00
[F] => 10.02.00
[A] => 10.00.00
[D] => 09.00.00
[E] => 08.00.00
[H] => 08.00.00
[I] => 07.00.00
[K] => 06.00.00
[J] => 06.00.00
)
我需要分配一些位置,但相同的分数将具有相同的位置,例如:
A : 1 -- draw score
B : 1 -- draw score
C : 1 -- draw score
D : 4 -- not 2, because the previous position
E : 5 -- normal
F : 6 -- normal
G : 7 -- draw score
H : 7 -- draw score
I : 9 -- not 8, because the previous position
...
我已经按分数尝试了小组项目,并从编号开始:
$result = array();
foreach($r as $k => $v)
{
$result[$k] = array_keys($r, $r[$k]);
}
$p = $c = 1;
foreach($result as $k => $v)
{
$ac = count($v);
if($ac==1)
{
$p = $p+$c;
$c = 1;
}
else
{
$c = $ac;
}
$result[$k] = $p;
}
print_r($result);
但我有这个:
Array
(
[C] => 1
[B] => 1
[G] => 1
[F] => 4
[A] => 5
[D] => 6
[E] => 6
[H] => 6
[I] => 8
[K] => 8
[J] => 8
)
我需要:
Array
(
[C] => 1
[B] => 1
[G] => 1
[F] => 4
[A] => 5
[D] => 6
[E] => 7
[H] => 7
[I] => 9
[K] => 10
[J] => 10
)
我如何正确排序?
答案 0 :(得分:2)
按值对结果进行分组,然后使用krsort
从最大到最小排序(注意:如果您的初始数组已经排序,则可以跳过krsort
)。通过迭代分组结果并按照每个组中的元素数增加分数来对结果进行评分。
$r = array (
'C' => '11.00.00',
'B' => '11.00.00',
'G' => '11.00.00',
'F' => '10.02.00',
'A' => '10.00.00',
'D' => '09.00.00',
'E' => '08.00.00',
'H' => '08.00.00',
'I' => '07.00.00',
'K' => '06.00.00',
'J' => '06.00.00',
);
$grouped_results = array();
foreach($r as $k => $v) {
$grouped_results[$v][] = $k;
}
krsort($grouped_results);
$score = 1;
$scores = array();
foreach($grouped_results as $group) {
foreach($group as $result) {
$scores[$result] = $score;
}
$score += count($group);
}
print_r($scores);
<强>输出强>
Array
(
[C] => 1
[B] => 1
[G] => 1
[F] => 4
[A] => 5
[D] => 6
[E] => 7
[H] => 7
[I] => 9
[K] => 10
[J] => 10
)
答案 1 :(得分:2)
$result = Array
(
"C" => "11.00.00",
"B" => "11.00.00",
"G" => "11.00.00",
"F" => "10.02.00",
"A" => "10.00.00",
"D" => "09.00.00",
"E" => "08.00.00",
"H" => "08.00.00",
"I" => "07.00.00",
"K" => "06.00.00",
"J" => "06.00.00"
);
$prev='';
$i=1;
foreach ($result as $key => $value) {
$ii = $i++;
if($value==$prev)
{
$tt[$key] = $tt[$p];
}else
{
$tt[$key] = $ii;
}
$prev = $value;
$p = $key;
}
echo '<pre>';
print_r($tt);
<强> 输出 强>
Array
(
[C] => 1
[B] => 1
[G] => 1
[F] => 4
[A] => 5
[D] => 6
[E] => 7
[H] => 7
[I] => 9
[K] => 10
[J] => 10
)