如何迭代多维数组并计算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.

评论
解决 无用
打赏 举报