简体   繁体   English

如何按日期(天)分组此mysql值在php中?

[英]How to group by date (day) this mysql values in php?

i have this tables in my mysql database: 我在我的mysql数据库中有此表:

+------------+-----------------------+--------+
| date       | payment_id            | total  |
+------------+-----------------------+--------+
| 2019-05-02 |                    37 |     56 |
| 2019-05-02 |                    52 |     70 |
| 2019-05-06 |                    37 |     60 |
| 2019-05-07 |                    43 |     63 |
| 2019-05-14 |                    43 |     66 |
| 2019-05-16 |                    37 |     87 |
| 2019-05-16 |                    43 |     83 |
| 2019-05-21 |                    43 |    100 |
| 2019-05-22 |                    52 |     27 |
| 2019-05-22 |                    37 |     27 |
+------------+-----------------------+--------+

my payment methods are: 我的付款方式是:

+----+-----------------------+------------------------+
| id | type                  | value                  |
+----+-----------------------+------------------------+
| 37 | type_of_payment       | Paypal                 |
| 38 | type_of_payment       | Wire transfer 30 days  |
| 39 | type_of_payment       | Wire transfer 30-60days|
| 43 | type_of_payment       | Credit card            |
| 51 | type_of_payment       | Cash on Delivery       |
| 52 | type_of_payment       | Stripe                 |
| 53 | type_of_payment       | Rc Banc                |
+----+-----------------------+------------------------+

my query from php are : 我从php查询是:

$query=QueryDB("SELECT date,total,value from table1 join table2 on table1.payment_id=table2.id where date between '2019-05-02' and '2019-05-06' order by date asc;");

where QueryDB is my method for query and return a value from database. 其中QueryDB是我的查询方法,并从数据库返回值。

the php object result is : php对象的结果是:

Array
(
    [0] => Array
        (
            [date] => 2019-05-02
            [0] => 2019-05-02
            [total] => 56
            [1] => 56
            [value] => Paypal
            [2] => Paypal
        )

    [1] => Array
        (
            [date] => 2019-05-02
            [0] => 2019-05-02
            [total] => 70
            [1] => 70
            [value] => Stripe
            [2] => Stripe
        )

    [2] => Array
        (
            [date] => 2019-05-06
            [0] => 2019-05-06
            [total] => 60
            [1] => 60
            [value] => PayPal
            [2] => Paypal
        )

 )       

the second query return all methods of payments available 第二个查询返回所有可用的付款方式

$paymentmethods=ObjectDB("select id,type,value from settings where type='type_of_
payment' order by value asc");

and i have this array 我有这个数组

Array (
[0] => Array
    (
        [value] =>Paypal                 
        [0] => Paypal
    )

[1] => Array
    (
        [value] => Wire transfer 30 days  
        [0] => Wire transfer 30 days  
    )

[2] => Array
    (
        [value] => Wire transfer 30-60days  
        [0] => Wire transfer 30-60days  
    )

[3] => Array
    (
        [value] => Credit Card
        [0] => Credit Card
    )

[4] => Array
    (
        [value] => Cash On Delivery
        [0] => Cash On Delivery
    )

[5] => Array
    (
        [value] => Stripe
        [0] => Stripe
    )

[6] => Array
    (
        [value] => Rc Banc
        [0] => Rc Banc
    )

)

I have structured my payment_methods_array with 我已经用以下方法构造了我的payment_methods_array

$payment_methods_array=array();
foreach($paymentmethods as $methods){
    $payment_methods_array[$methods['value']]=0;
}

And i have structured the desiderated output array with: 我用以下方法构造了讨厌的输出数组:

$days=array();
foreach($query as $day){
    $key=$day['value'];
    $date=$day['date'];
    $total=$day['total'];
    $payment_methods_array[$key]=$total;
    $days[$date]=$payment_methods_array;
}

The output array is: 输出数组为:

Array(
[2019-05-02] => Array
    (
        [Wire transfer 30 days] => 0
        [Stripe] => 70
        [Wire transfer 30-60days] => 0
        [Paypal] => 56
        [Rc Banc] => 0
        [Credit card] => 0
        [Cash on Delivery] => 0
    )

[2019-05-06] => Array
    (
        [Wire transfer 30 days] => 0
        [Stripe] => 70
        [Wire transfer 30-60days] => 0
        [Paypal] => 60
        [Rc Banc] => 0
        [Credit card] => 0
        [Cash on Delivery] => 0
    )
)

the problem is in the second array: The payment with stripe in query result is 0 but in the output array is the previous array value 70. can i solve this? 问题出在第二个数组中:查询结果中带有条带的付款为0,但在输出数组中为前一个数组值70。我能解决这个问题吗? thanks. 谢谢。

You are reusing $payment_methods_array again and again. 您一次又一次重复使用$payment_methods_array You need to reset it's values to 0 for every day. 您需要每天将其值重置为0 However - I would rather initialize the result with zeros for all days and then only change the values which you get from the DB. 但是-我宁愿整天都用零初始化结果,然后只更改从数据库中获得的值。

Try the following: 请尝试以下操作:

$payment_methods_array=array();
foreach($paymentmethods as $methods){
    $payment_methods_array[$methods['value']]=0;
}

$days = [];

// init zero values
foreach ($query as $day) {
    $days[$day['date']] = $payment_methods_array;
}

// fill db values
foreach ($query as $day) {
    $days[$day['date']][$day['value']] = $day['total'];
}

The result: 结果:

array (
  '2019-05-02' => 
  array (
    'Paypal' => 56,
    'Wire transfer 30 days ' => 0,
    'Wire transfer 30-60days' => 0,
    'Credit Card' => 0,
    'Cash On Delivery' => 0,
    'Stripe' => 70,
    'Rc Bancv' => 0,
  ),
  '2019-05-06' => 
  array (
    'Paypal' => 0,
    'Wire transfer 30 days ' => 0,
    'Wire transfer 30-60days' => 0,
    'Credit Card' => 0,
    'Cash On Delivery' => 0,
    'Stripe' => 0,
    'Rc Bancv' => 0,
    'PayPal' => 60,
  ),
)

demo on rextester.com 在rextester.com上进行演示

This might be not the most effitient way, but it's quite short and simple. 这可能不是最有效的方法,但它非常简短。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM