如何将foreach循环最后一个值保存到变量中

时间:2015-09-08 09:18:36

标签: php pdf-generation foreach-loop-container

如何将$total['value']的最后一个值存储到$COD

变量并回显它和页面代码的其他位置

 <?php foreach( $wpo_wcpdf->get_woocommerce_totals() as $key => $total ) : ?>
    <tr class="<?php echo $key; ?>">
        <td class="no-borders">&nbsp;</td>
        <th class="description"><?php echo $total['label']; ?></th>
        <td class="price"><span class="totals-price"><?php echo $total['value']; ?></span></td>
    </tr>   
<?php endforeach; ?>

此代码&#34; WooCommerce PDF发票&amp;装箱单&#34;免费插件

2 个答案:

答案 0 :(得分:1)

试试这个,值会被覆盖,最终你会有最后一个值

$COD = $total['value'];

答案 1 :(得分:0)

我使用此

解决它
  

if($ key ==='order_total'){$ COD = $ total ['value']; }

<?php foreach( $wpo_wcpdf->get_woocommerce_totals($grand_total) as $key => $total ) : ?>
                    <tr class="<?php echo $key; ?>">
                        <td class="no-borders">&nbsp;</td> 
                        <th class="description"><?php echo $total['label']; ?></th>
                        <?php if ($key === 'order_total'){ $COD = $total['value']; } ?>

                        <td class="price"><span class="totals-price"><?php echo $total['value']; ?></span></td>
                    </tr>   
                    <?php endforeach; ?>

谢谢@ Zl3n 谢谢Stackoverflow