PHP求和数组 - 仅对数组元素求和

4

我有一个数组,如下所示:

$row 当我输出整个数组时,它会产生以下结果:

1. I like crisps (38) 37% 55% 8% 0%

当我echo数组的一部分时,我得到了我感兴趣的4个数字。

echo "<td class='set1'>". number_format($row[6], 0) ."%</td>";

上面的代码输出了以下4个数字:

37% 55% 8% 0%

我想做的就是简单地将前两个数字相加(即37% + 55%),并输出结果(92%)。希望这有所帮助?

我还应该指出,数组包含的信息比这四个数字要多得多。

按照要求提供:var_dump[6]的输出

string(7) "36.8421" string(7) "28.9474" string(7) "39.4737" string(7) "23.6842" string(7) "28.9474" string(6) "8.0000" string(7) "23.6842" string(7) "39.4737" string(7) "11.1111" string(7) "13.8889" string(7) "11.1111" string(7) "13.8889" string(7) "17.1429" string(7) "20.0000" string(7) "28.5714" string(7) "25.7143" string(7) "34.2857" string(7) "28.5714" string(7) "28.5714" string(7) "28.5714" string(7) "20.5882" string(7) "20.5882" string(7) "11.7647" string(7) "29.4118" string(7) "17.6471" string(7) "20.5882" string(6) "3.0303" string(6) "2.9412" string(6) "3.0303" string(7) "38.2353" string(7) "12.1212" string(7) "27.2727" string(7) "18.1818" string(7) "33.3333" string(7) "34.7826" string(7) "17.3913" string(7) "30.4348" string(7) "17.3913" string(7) "17.3913" string(7) "13.0435" string(7) "30.4348" string(7) "27.2727"

有没有办法做到这一点?

提前感谢。

霍默。

完整代码 - 希望这能帮到你:

if ($mysqli->multi_query($query)) {
do {
    /* store first result set */
    if ($result = $mysqli->store_result()) {
        $i = 1;
        while ($row = $result->fetch_row()) {
            if($i==1){
            echo "<tr><td class='qnum'><span class='bold'>". $row[3] .".</span></td><td class='qtext'> ". $row[4] ." (<span class='italics'>". $row[5] ."</span>)</td><td></td>";
            $i = 0;
            }

            echo "<td class='set1'>". number_format($row[6], 0) ."%</td>";
            $var =  $row[6];
        }
        echo "</tr>";
        $result->free();
    }
    /* print divider */
    //if ($mysqli->more_results()) {
      //  printf("-----------------\n");
    //}
} while ($mysqli->next_result());
}

出现$row[6]的部分是四个数字37% 55% 8% 0%,我想把前两个数字加在一起。

如果有帮助,这是SQL查询结果。

response    q1  Responses   qnum    Question_Text   Total   percentage
4           4   14          1       I like crisps   38    36.8421
3           3   21          1       I like crisps   38    55.2632
2           2   3           1       I like crisps   38    7.8947
1           NULL    0       1       I like crisps   38    0.0000

我想要做的是将36.8421和55.2632相加。
希望这有所帮助!

你能展示一下你编写的实现上述功能的代码吗?这样可能更容易提供解决方案。 - anonymous
echo "<td class='set1'>". number_format($row[6], 0) ."%</td>";以上代码输出以下4个数字:37% 55% 8% 0%我想要做的就是将前两个数字简单相加(即37% + 55%),并输出结果(92%)。希望这可以帮到您?谢谢。 - Homer_J
请编辑问题而不是评论。评论没有格式。此外,您可以添加var_dump($row)var_dump($row[6])的输出以更准确地描述数据结构,以获得更好的响应。 - nikc.org
@nikc - 看上面 - 是的,注意到在注释中放置代码 - 我也可以为 $row 生成转储,但请注意,由于数组正在通过循环,所以有大量的数据 - 我认为只需处理循环的一个迭代会更容易。 - Homer_J
看起来,每行都有一个值。(假设您正在使用foreach循环集合。)我说的对吗?你想要什么样的总和?每两行一组?就像$rows[n][6] + $rows[n+1][6]这样吗? - nikc.org
显示剩余2条评论
2个回答

7

我想你可以使用 array_sumarray_slice 的组合:

$arr = array(1, 2, 3, 4);
echo "First sum: " . array_sum(array_slice($arr,0,2));
echo '<br />';
echo "Second sum: " . array_sum(array_slice($arr,2,3));

输出:

First sum: 3
Second sum: 7

根据更新后的问题,您需要类似以下内容:

$str = "55% 23% 12% 20%";

// the below line can be omitted because of the way PHP handles conversions
$str = str_replace('%', '', $str);
$arr = split(' ', $str);
echo "First sum: " . array_sum(array_slice($arr,0,2)) . '%';
echo '<br />';
echo "Second sum: " . array_sum(array_slice($arr,2,3)) . '%';

输出:

First sum: 78%
Second sum: 32%

我似乎无法让这个工作起来 - 我觉得原因是我正在处理数组$row[6]的特定部分 - 上面的例子使用了整个数组$arr?除非我理解错了什么,这种可能性非常非常大! - Homer_J
@Homer_J - 上面的代码是为了提供一个自包含的工作示例。只需将 $arr 更改为 $row[6] 即可。 - karim79
谢谢留言 - 我已经尝试用 $row[6] 替换 $arr,但它根本没有返回任何内容,只是空白。 - Homer_J
再次感谢您抽出时间回复,但那也行不通——因为我在数组中存储的不仅仅是四个数字,所以我使用 $row[6] 从数组中获取这四个数字——但还有其他的东西。 - Homer_J

2

假设您总是想要对每两行进行求和,则可以按照以下方式执行:

// This for-loop is intended as a replacement for your 
// innermost while-loop (rows 6-14 in your question).
// I used a for-construct because we need to count the 
// cycles, and it will do it for us.
// The condition for terminating the loop is still the same
// as in the original while-loop.

?><tr><?php
for ($rownum = 1; $row = $result->fetch_row(); $rownum++) {
    if ($rownum == 1) {
        ?>
        <td class='qnum'><span class='bold'><?php echo $row[3] ?></span></td>
        <td class='qtext'><?php echo $row[4] ?> (<span class='italics'><?php echo $row[5] ?></span>)</td><td></td>
        <?php
    }

    // Sum and display on even cycles 
    // as <N> modulo 2 == 0 is true only on even numbers
    if ($rownum % 2 == 0) {
        // Display the formatted value,
        // at the same time adding the previous cycles value to this one
        ?><td class='set1'><?php echo number_format($tmp + $row[6], 0) ?>%</td><?php

    // Store the value of $row[6] on odd cycles
    } else {
        $tmp = $row[6];
    }
}
?></tr><?php

@nikc - 感谢您抽出时间,但我不确定上面的代码如何与我的现有代码配合 - 很遗憾,我无法理解它。看起来它不能给我我所需要的东西。 - Homer_J
@Homer:它旨在替换您最内层的while循环。我添加了一些注释(希望)澄清我的意思。 - nikc.org

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接