2010-04-21 77 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