Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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_Foreach_Count - Fatal编程技术网

Php 外汇总价值

Php 外汇总价值,php,loops,foreach,count,Php,Loops,Foreach,Count,请帮助完成此功能,我想在这里计算$answer的总值 <?php foreach($_POST as $fieldName=> $answer){ if($answer=="1"){settype($answer, "integer"); if(is_int($answer)){ //I want to echo the total value of $answer. Right now it gives me "1111" instead of "4

请帮助完成此功能,我想在这里计算$answer的总值

<?php 
  foreach($_POST as $fieldName=> $answer){
   if($answer=="1"){settype($answer, "integer");
   if(is_int($answer)){  
     //I want to echo the total value of $answer. Right now it gives me "1111" instead of "4"       and I want 4.
   }
?>

PHP语言是多类型的,因此您可以简化代码以检查值:

$total = 0;
foreach($_POST as $fieldName => $answer)
{
    ( $answer == (int) $answer ) && $total+= $answer;
}
我没弄错你的问题吧?

你可以试试这个

<?php $myTotal = 0;
  foreach($_POST as $fieldName=> $answer){
   if($answer=="1"){settype($answer, "integer"); }
       if(is_int($answer)){  
          $myTotal +=$answer;
       }
   }
?>


您能否至少发布初始数组值?非常感谢您的快速响应!!!所有答案都有效!!!非常感谢!没问题。如果你的问题解决了,请接受你认为最好的答案,这样问题就不再是“公开的”问题了。
<?php $myTotal = 0;
  foreach($_POST as $fieldName=> $answer){
   if($answer=="1"){settype($answer, "integer"); }
       if(is_int($answer)){  
          $myTotal +=$answer;
       }
   }
?>