PHP Sum Array - bir dizinin toplamı sadece elemants

2 Cevap

Ben aşağıdaki gibi bir dizi var var:

$row zaman çıktısı aşağıdaki ürettiği tüm dizi:

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

Ben dizinin echo bir parça ben ilgilendiğim 4 rakamlar olsun

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

Yukarıdaki kod aşağıdaki 4 rakamları verir:

% 37% 55% 8% 0

Ne yapmak istiyorum sadece reklam ilk iki birlikte sayı (yani% 37 +% 55) ve çıkış sonuç (% 92) 'dir. Umut olur?

Ben de dizi sadece bu dört rakamlar çok daha fazla bilgi içerdiğine işaret etmelidir.

Talep gibi: var_dump[6] çıkışı

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"

Bunu yapmak için herhangi bir yolu var mı?

Teşekkür peşin,

Homer.

Tüm kodu - Bu yardımcı olur umarım:

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());
}

Dört firgures 37% 55% 8% 0% nerede görüneceğini $ satır [6] görünen bölümü - Ben birlikte bu rakamlar ilk iki eklemek istiyorum.

Bu yardımcı olur, burada SQL sorgusunun sonucudur

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

Ne yapmak istiyorum 36,8421 ve 55,2632 birlikte reklamdır.

Bu yardımcı olur umarım!

2 Cevap

Aşağıdaki gibi always toplamak istediğiniz varsayımına dayanarak every two rows, bunu yapabilirsin:

// 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