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!

图片转代码服务由CSDN问答提供 功能建议

问题:这就是排名。 此等级应包含从零到十的数字。 但是,我注意到存储值的数组的总和不会输入高于07的数字。因此,它只能将数字从00到07加起来! 我找不到错误。

代码:

 &lt; tbody&gt; 
&lt;?  php 
 $ scores = array(
“Kyle”=&gt; array(“ - ”,04,04,06,04,05,04,04),
“Bill”=&gt; array(“ - ”  ,04,09,04,07,08,04,10),
“Adam”=&gt;数组(“ - ”,04,04,04,01,04,04,04),
“Johnny”  =&gt;数组(“ - ”,08,04,10,04,08,08,04),
?&gt; 
 
 ###函数返回sum 
function cmp($ a,$ b  ){
返回array_sum($ b) -  array_sum($ a); 
} 
 
 ####将排名排序到td 
uasort($ scores,“cmp”); 
foreach($ score)  as $ key =&gt; $ value){
 echo“&lt; tr&gt;”; 
 echo“&lt; th&gt; $ key&lt; / th&gt;”; 
 echo“&lt; td class ='total'align =  'center'valign ='middle'&gt;“。array_sum($ value)。”&lt; / td&gt;“; 
 for($ x = 0; $ x&lt; count($ value); $ x ++){
  echo“&lt; td class ='games tip'align ='center'valign ='middle'&gt;”。sprintf('%02d',$ value [$ x])。“&lt; / td&gt;”; 
  } 
 echo“&lt; / tr&gt;”; 
} 
?&gt; 
&lt; / tbody&gt; 
   
 
 

谢谢!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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条)

相关推荐 更多相似问题