php - 如何在数组元素的最后一个实例之后输出小计

标签 php mysql arrays

我正在从 MySQL 数据库创建一份报告,该报告将显示会计企业客户所花费的时间。我有一个名为 $test 的数组,其中包含报告的信息。该数组已在 [sort] 上按字母顺序排序。

我使用 foreach 将此数组回显到 html 表。如何在每个客户之后输出小计。正如您从下面的 $test 示例中看到的,有多个名为 JOHN Q ABERNATHY 的客户端实例。在本例中,我需要在第三个 JOHN Q ABERNATHY 之后回显小计。

Array
(
    [0] => Array
        (
            [sort] => ABERNATHY, JOHN Q
            [date] => 07-23-2015
            [client] => JOHN Q ABERNATHY
            [description] => test
            [user_name] => Jason
            [time_spent] => 00:10
            [amount] => 12.50
            [end] => 0
        )

    [1] => Array
        (
            [sort] => ABERNATHY, JOHN Q
            [date] => 07-23-2015
            [client] => JOHN Q ABERNATHY
            [description] => changed again
            [user_name] => Jason
            [time_spent] => 03:14
            [amount] => 242.50
            [end] => 0
        )

    [2] => Array
        (
            [sort] => ABERNATHY, JOHN Q
            [date] => 07-23-2015
            [client] => JOHN Q ABERNATHY
            [description] => time test again
            [user_name] => Jason
            [time_spent] => 00:53
            [amount] => 66.25
            [end] => 0
        )

    [3] => Array
        (
            [sort] => BAUMAN, TERRY L
            [date] => 07-23-2015
            [client] => TERRY L BAUMAN
            [description] => test ind
            [user_name] => Jerry
            [time_spent] => 00:00
            [amount] => 0.00
            [end] => 0
        )

    [4] => Array
        (
            [sort] => Bethany Baptist
            [date] => 07-01-2015
            [client] => Bethany Baptist
            [description] => lots of work
            [user_name] => Jason
            [time_spent] => 05:25
            [amount] => 406.25
            [end] => 0
        )

    [5] => Array
        (
            [sort] => Johnson Mortuary Service, LLC
            [date] => 07-31-2015
            [client] => Johnson Mortuary Service, LLC
            [description] => pr
            [user_name] => Jason
            [time_spent] => 00:04
            [amount] => 5.00
            [end] => 0
        )

    [6] => Array
        (
            [sort] => COLEMAN, MIKE R
            [date] => 07-31-2015
            [client] => MIKE R COLEMAN
            [description] => PHONE CONCERNING PAYMENT PLAN
            [user_name] => Jason
            [time_spent] => 00:20
            [amount] => 25.00
            [end] => 0
        )

我的小计存储在名为 $sum 的数组中。这是我的代码的一部分,我将小计存储到 $sum 并将 $test 回显到表中:

    $sum = array();
    $end = array();
    foreach($test as $key => $value) {

        //The following code puts subtotals of the $test array into a new array called $sum
        if (!isset($sum[$value['sort']])) {
            $sum[$value['sort']] = 0;
        }
        $sum[$value['sort']] += $value['amount'];
        //end of summing code

        //This is where I am trying to create the subtotal but obviously have not gotten very far

        if (!isset($end[$value['sort']])) {
            $end[$value['sort']] = 0;
            prev($test);

            $end[$value['sort']] = 1;

            next($test);
        }


        $date2=$value['date'];
        $client2=$value['client'];
        $description2=$value['description'];
        $user_name2=$value['user_name'];
        $time_spent2=$value['time_spent'];
        $amount2=$value['amount'];

        echo 
        "
            <tr>
                <td>$date2</td>
                <td>$client2</td>
                <td>$description2</td>
                <td>$user_name2</td>
                <td>$time_spent2</td>
                <td class='text-right'>$$amount2</td>
            </tr>
        ";  
    }

我如何知道 foreach 何时循环了 JOHN Q ABERNATHY 的最后一个实例,以便我可以回显他的总 [金额]?

最佳答案

试试这个:

  $sum = 0;
    $first = true;
    echo "<Table border='1'>";
    foreach($test as $key => $value) {

        if(!$first && $oldClient !== $value['client'])
        {
            //Sub-total info goes here.
            echo
            "
                <tr>
                    <td colspan='6'>$oldClient $sum</td>
                </tr>
            ";

            //Reset the sum variable
            $sum = 0;
        }

        $oldClient = $value['client'];

        $sum += $value['amount'];

        $date2=$value['date'];
        $client2=$value['client'];
        $description2=$value['description'];
        $user_name2=$value['user_name'];
        $time_spent2=$value['time_spent'];
        $amount2=$value['amount'];

        echo
        "
                <tr>
                    <td>$date2</td>
                    <td>$client2</td>
                    <td>$description2</td>
                    <td>$user_name2</td>
                    <td>$time_spent2</td>
                    <td class='text-right'>$$amount2</td>
                </tr>
            ";
        $first = false;
    }

    //Sub-total info goes here as well to handle the last group.    
    echo
    "
                <tr>
                    <td colspan='6'>$oldClient $sum</td>
                </tr>
            ";

    echo '</table>';

在每个循环中都会发生以下情况:

  • 客户端名称设置为临时变量
  • 临时客户端变量 $oldCLient 与 $value['client'] 进行比较。如果它们不匹配并且不是第一个循环,则意味着已经通过了一组相同的客户端,并且是时候打印小计了。

请注意,您还必须

  • 在 foreach 循环后打印最后一个客户组的小计
  • 我已将总和从数组更改为整数。每个客户组的小计打印到屏幕后,它会重置为 0
  • 我还删除了“end”数组。不需要。

关于php - 如何在数组元素的最后一个实例之后输出小计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31839028/

相关文章:

php - Slim Framework 中的安装文件 .env

php - 编译 html(页眉和页脚)

java - 一维数组遇到问题

objective-c - 初始化 'Method *' C 时不兼容的指针类型

php - Android数组转mysql数据库?

arrays - swift 的数组范围

php - 错误: Can't connect to local MySQL server through socket

php - 如何使用 AJAX 从复选框获取值并传递到 PHP 页面

mysql - Magento 1.7 错误 : SQLSTATE[HY000] [2002] No such file or directory Sometimes Only

mysql从时间戳中获取月份不工作