如何从所有逻辑值或PHP中的数组中获取总计

时间:2017-08-23 09:45:14

标签: php

如何从逻辑或数组值中获取总数

I have an asset table
Asset
purchase_date
price
depreciation_age
depreciation_cost_monthly

这是报告

Asset           Purchase Date     price          depreciation age        depreciation cost/ month         Asset Age         depreciation accumulation
Computer        2016-01-01        10000            40 (month)                   250                        20 (month)              5000
Printer         2016-12-01        50000            36 (month)                   139                        8 (month)               1112

此代码用于计算资产年龄

    <?php $date = date("Y-m-d");
     $timeStart = strtotime($thomas['purchase_date']);
    $timeEnd = strtotime("$date");
    $numMonth = 0 + (date("Y",$timeEnd)-date("Y",$timeStart))*12;
    $numMonth += date("m",$timeEnd)-date("m",$timeStart);

     echo $numMonth;
     ?>

此代码用于计算折旧累计

        <?php 
         $one = ($thomas['depreciation_cost_monthly']);
        $two = $numMonth;
        $accumulation = $one * $two;
        echo $accumulation;    
        ?>

我需要帮助来总结所有积累值,我有代码低于但它不起作用

    <?php
    $three = array ($accumulation);
       $total = array_sum ($three);
    Echo $total;  ?>

1 个答案:

答案 0 :(得分:0)

只需在$accumulation变量中添加$three值,如下所示

<?php 
$one = ($thomas['depreciation_cost_monthly']);
$two = $numMonth;
$accumulation = $one * $two;
echo $accumulation;
$three[] = $accumulation;
?>

并汇总$three数组以获得总数

<?php echo array_sum($three); ?>