Php 外汇总价值
请帮助完成此功能,我想在这里计算$answer的总值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
<?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;
}
}
?>