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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!