PHP Sum Array - 仅对数组的元素求和

Hom*_*r_J 4 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());
}
Run Code Online (Sandbox Code Playgroud)

$ 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
Run Code Online (Sandbox Code Playgroud)

我想做的是将36.8421和55.2632加在一起.

希望这可以帮助!

kar*_*m79 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));
Run Code Online (Sandbox Code Playgroud)

输出:

First sum: 3
Second sum: 7
Run Code Online (Sandbox Code Playgroud)

编辑:根据更新的问题,你需要这样的东西:

$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)) . '%';
Run Code Online (Sandbox Code Playgroud)

输出:

First sum: 78%
Second sum: 32%
Run Code Online (Sandbox Code Playgroud)