doulipi3742 2015-06-19 21:49
浏览 54
已采纳

如何迭代多维数组并计算php中每行的总和?

i have this array which i retrieved data from my database and put it in an array :

$query = "select * from caseunder"; 
$result=mysql_query($query) or die('Error, insert query failed'); 
$array[] = array();
$numrows=mysql_num_rows($result); 
WHILE ($i < $numrows){ 
    $allergies =mysql_result($result, $i, "allergies");  
    $vege = mysql_result($result,$i, "vege");  
    $age = mysql_result($result, $i, "age");  
    $bmi =mysql_result($result, $i, "bmi"); 
    $solution = mysql_result($result,$i, "solution"); 
    $bmi2 = $_POST['weight'] / (($_POST['height']/100)*($_POST['height']/100));
    if($_POST['age']>18 && $_POST['age']<35)
        $age2 = 'young ';
    if($_POST['age']>40 && $_POST['age']<50)
        $age2 = 'middle age ';
    if($_POST['age']>60)
        $age2 = 'old men ';
    $array[] = array('age'=>$age2,'allergies'=>$allergies,'vege'=>$vege,'bmi'=>$bmi2,'solution'=>$solution);

    i++
}

Then, i want to compare each element in that array with input i entered and calculate sum for each row of array :

foreach($array as $cases) {

    if($cases['allergies'] == $_POST['allergies']){
        $count = 1;
    }

    if($cases['vege'] == $_POST['vege']){
        $count1 = 1;
    }

    if($cases['bmi'] == $bmi2)
        $count2 = 1;

    if($cases['age'] == $age2)
        $count3 = 1;

    $sum = $count + $count1 + $count2 + $count3;
    echo $sum;
}

Lets say i have entered age,bmi, allergies and vege which is all are the same like first row of database, the the total sum that should be output is 4 because 4 same comparison of data. In this case that i try, every row of database should have different total sum because its not all the same.But i did not get the output that i want, this is the example of the wrong output:

0
4
4
4
4
4
4
4
4

(assuming i have 8 rows of database in my phpmyadmin) The first row of database after compared manually the sum is 4 but it seems like when it continue looping the next row take the same amount as prev row.

  • 写回答

3条回答 默认 最新

  • doudang4568 2015-06-19 23:15
    关注

    When you do this loop:

    foreach($array as $cases) {
    
        if($cases['allergies'] == $_POST['allergies']){
            $count = 1;
        }
        if($cases['vege'] == $_POST['vege']){
            $count1 = 1;
        }
        if($cases['bmi'] == $bmi2)
            $count2 = 1;
        if($cases['age'] == $age2)
            $count3 = 1;
        $sum = $count + $count1 + $count2 + $count3;
        echo $sum;
    }
    

    You are not resetting the $count, $count1, etc. variables between iterations. That is why

    when it continue looping the next row take the same amount as prev row.

    I would say you probably don't even need these separate variables unless you are using them for something else that isn't included in the question. You can just initialize sum to zero for each repetition, then increment it directly if the conditions match.

    foreach($array as $cases) {
        $sum = 0;   
        if($cases['allergies'] == $_POST['allergies']){
            $sum++;
        }
        if($cases['vege'] == $_POST['vege']){
            $sum++;
        }
        if($cases['bmi'] == $bmi2) {
            $sum++;
        }
        if($cases['age'] == $age2) {
            $sum++;
        }
        echo $sum;
    }
    

    Incidentally, it looks like the way you are setting bmi and age in the first loop will make those values always match. I'm not sure if that's what you intended, but it seems kind of unlikely.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址