Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 编号结果列表,如果次数相等,则保留位置编号_Php_Loops - Fatal编程技术网

Php 编号结果列表,如果次数相等,则保留位置编号

Php 编号结果列表,如果次数相等,则保留位置编号,php,loops,Php,Loops,我有一个结果表,我对它进行迭代,然后进行回音 $c = 1; foreach ($results as $result) { $r .= '<tr>' . '<th scope="row">' . ($time === $result['time']? $c - 1 : $c) . '</th>' . '<td>' . $result['name'] . '</td>' . '<td>' .

我有一个结果表,我对它进行迭代,然后进行回音

$c = 1;
foreach ($results as $result) {
    $r .= '<tr>'
    . '<th scope="row">' . ($time === $result['time']? $c - 1 : $c)  . '</th>'
    . '<td>' . $result['name'] . '</td>'
    . '<td>' . $result['time'] . ' </td>'
    . '<td>' . $result['points'] . ' </td>'
    . '</tr>';

    $time = $result['time'];
    $c++;
}
$c=1;
foreach($results作为$result){
$r.=''
“。”($time===$result['time']?$c-1:$c)。”
“.$result['name']”
“.$result['time']”
“.$result['points']”
. '';
$time=$result['time'];
$c++;
}
我将当前时间与以前的结果时间进行比较,如果它们匹配,则显示相同的计数。 e、 g.
1.汤姆0.33
2.Ben 0.34
2.卡尔0.34
4.Des 0.35
5.戴夫0.36

但是如果Des也得到了0.34呢?它将显示计数3,并应保持在2


有没有办法在不太复杂的情况下解决这个问题?

这是因为它只会递减1

$c - 1
您可能需要一个像您现在这样的计数器,但需要一个当前行。差不多

$c = 1;
$curr = $c;
foreach( $results as $result){
  if($time === $result['time']){
    use $curr;
  }else{
    use $c;
    $curr = $c + 1;
  }
  $c++;
}
但是如果移动$c++,可以将其与$curr绑定,如

$c++;
$curr = $c;
$c=1;
$lastC=$c;
foreach($results作为$result){
如果($time===$result['time'])){
$place=$lastC;
}否则{
$place=$c;
$lastC=$c;
}
$r.=''
“.$place.”
“.$result['name']”
“.$result['time']”
“.$result['points']”
. '';
$time=$result['time'];
$c++;
}

您可以在短if中按时进行数学运算。您必须使结果永久化:

$c = 1;
foreach ($results as $result) {
    if($time === $result['time'])
      $c--;
    $r .= '<tr>'
    . '<th scope="row">' . $c  . '</th>'
    . '<td>' . $result['name'] . '</td>'
    . '<td>' . $result['time'] . ' </td>'
    . '<td>' . $result['points'] . ' </td>'
    . '</tr>';

    $time = $result['time'];
    $c++;
}
$c=1;
foreach($results作为$result){
如果($time===$result['time']))
$c--;
$r.=''
“.$c.”
“.$result['name']”
“.$result['time']”
“.$result['points']”
. '';
$time=$result['time'];
$c++;
}
函数比较($1,$2)
{
$a=$1[‘时间’];
$b=$2[‘时间’];
如果($a=$b){
返回0;
}
回报($a<$b)?-1:+1;
}
$array=array(
数组('name'=>'Tom','time'=>'0.33','points'=>1),
数组('name'=>'Ben','time'=>'0.36','points'=>1),
数组('name'=>'Carl','time'=>'0.35','points'=>1),
数组('name'=>'Des','time'=>'0.33','points'=>1),
数组('name'=>'Dave','time'=>'0.34','points'=>1)
);
usort($array,'compare');
打印(数组);

嗯,对不起。那是我的错。我认为这将是理想的结果。如果有多人同时跑步,这项工作会怎么样?我觉得lastC会落后(我可能错了),我很确定这并不能回答这个问题。我猜询问者在他们需要帮助的代码之前已经有了一个排序机制。我应该想到这一点。在else语句中,您不需要将$c增加1,因此您可以回答@Tro.np。我本应该把它的格式设置得跟在你的答案后面,但至少我对你有帮助。:)祝你有美好的一天
$c = 1;
foreach ($results as $result) {
    if($time === $result['time'])
      $c--;
    $r .= '<tr>'
    . '<th scope="row">' . $c  . '</th>'
    . '<td>' . $result['name'] . '</td>'
    . '<td>' . $result['time'] . ' </td>'
    . '<td>' . $result['points'] . ' </td>'
    . '</tr>';

    $time = $result['time'];
    $c++;
}
function compare($one, $two)
{
    $a = $one['time'];
    $b = $two['time'];

    if ($a == $b) {
        return 0;
    }

    return ($a < $b) ? -1 : +1;
}


$array = array(
    array('name' => 'Tom', 'time' => '0.33', 'points' => 1),
    array('name' => 'Ben', 'time' => '0.36', 'points' => 1),
    array('name' => 'Carl', 'time' => '0.35', 'points' => 1),
    array('name' => 'Des', 'time' => '0.33', 'points' => 1),
    array('name' => 'Dave', 'time' => '0.34', 'points' => 1)
);

usort($array, 'compare');

print_r($array);