2010-04-21 64 views
4

我有一個數組如下的elemants:PHP薩姆陣列 - 總和僅陣列

$row當I輸出這將產生以下的整個陣列:

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

其中$行[6]出現是該部分,其中四個firgures 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在一起。

希望這會有所幫助!

+0

你能不能告訴我們,你寫來實現一些代碼以上?以這種方式提供解決方案可能更容易。 – anonymous 2010-04-21 09:37:36

+0

好的 - 在這裏! echo「」。 number_format($ row [6],0)。「%」; 上面的代碼輸出以下4個數字: 37%\t 55%\t 8%\t 0% 我想這樣做簡單地是廣告的前兩個數字相加(即37%+ 55%)和輸出結果(92%)。 希望有幫助嗎? 在此先感謝, 荷馬。 – 2010-04-21 11:04:32

+0

請編輯問題而不是評論。評論沒有格式。另外,您可以添加'var_dump($ row)'或'var_dump($ row [6])'輸出來更準確地描述您的數據結構,以獲得更好的響應。 – 2010-04-21 12:03:49

回答

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 
+0

@nikc - 感謝您抽出寶貴的時間,但我不確定上述內容如何適合我現有的代碼 - 我無法理解這一點。看着它,它不會給我我需要的東西。 – 2010-04-21 13:22:14

+0

@Homer:它的目的是取代你最內層的'while'循環。我添加了一些評論(希望)澄清我的意思。 – 2010-04-21 14:39:24

8

我想你可以使用的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% 
+0

我似乎無法完成這項工作 - 我認爲原因是我正在處理數組$行的特定部分[6] - 上面的例子是使用整個數組$ arr? 除非我不能正確理解某些東西,這很可能! – 2010-04-21 10:23:27

+0

@Homer_J - 上述意圖是一個獨立的工作示例。只要將'$ arr'改爲'$ row [6]'就可以了。 – karim79 2010-04-21 11:52:58

+0

感謝您的提示 - 我曾嘗試用$ row [6]替換$ arr,但它不會帶回任何內容,只是空白。 – 2010-04-21 12:16:37