donxbje866688 2014-06-26 22:46
浏览 40
已采纳

数组不总和高于7的数字

Problem: This is all about ranking. This rank should contain numbers from zero up to ten. However, I notice that the sum of my array that stores the values are not entering numbers higher than 07. So, it can only sums the number from 00 up to 07! I couldn't find the mistake.

Code:

<tbody>
<?php
  $scores = array(
  "Kyle"   => array("-", 04, 04, 06, 04, 05, 04, 04),
  "Bill"   => array("-", 04, 09, 04, 07, 08, 04, 10),
  "Adam"   => array("-", 04, 04, 04, 01, 04, 04, 04),
  "Johnny" => array("-", 08, 04, 10, 04, 08, 08, 04),
?>

###function to return the sum
function cmp($a, $b) {
  return array_sum($b) - array_sum($a);
}

####sort the ranking into the td
uasort($scores, "cmp");
foreach($scores as $key=>$value) {
  echo "<tr>";
  echo "<th>$key</th>";
  echo "<td class='total' align='center' valign='middle'>" . array_sum($value) . "</td>";
  for($x=0; $x<count($value); $x++) {
    echo "<td class='games tip' align='center' valign='middle'>".sprintf('%02d', $value[$x])."</td>";
  }
  echo "</tr>";
}
?>
</tbody>

Thank you!

  • 写回答

2条回答 默认 最新

  • dtrb96410 2014-06-26 22:51
    关注

    Numbers beginning with 0 are treated as octal numbers, and may only contain digits from 0 to 7. Any other digit is treated as a delimiter, not part of the number. So 08 and 09 are interpreted as 0.

    Get rid of all you leading digits:

      $scores = array(
      "Kyle"   => array("-", 4, 4, 6, 4, 5, 4, 4),
      "Bill"   => array("-", 4, 9, 4, 7, 8, 4, 10),
      "Adam"   => array("-", 4, 4, 4, 1, 4, 4, 4),
      "Johnny" => array("-", 8, 4, 10, 4, 8, 8, 4),
      );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料