PHP Sum Array - 求和数组的所有元素

时间:2010-04-21 09:35:15

标签: php

我有一个数组如下:

$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加在一起。

希望这有帮助!

2 个答案:

答案 0 :(得分: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%

答案 1 :(得分: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