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

如何迭代多维数组并计算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 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档