簡體   English   中英

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

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

我在我的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 |
+------------+-----------------------+--------+

我的付款方式是:

+----+-----------------------+------------------------+
| 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                |
+----+-----------------------+------------------------+

我從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;");

其中QueryDB是我的查詢方法,並從數據庫返回值。

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
        )

 )       

第二個查詢返回所有可用的付款方式

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

我有這個數組

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
    )

)

我已經用以下方法構造了我的payment_methods_array

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

我用以下方法構造了討厭的輸出數組:

$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;
}

輸出數組為:

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
    )
)

問題出在第二個數組中:查詢結果中帶有條帶的付款為0,但在輸出數組中為前一個數組值70。我能解決這個問題嗎? 謝謝。

您一次又一次重復使用$payment_methods_array 您需要每天將其值重置為0 但是-我寧願整天都用零初始化結果,然后只更改從數據庫中獲得的值。

請嘗試以下操作:

$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'];
}

結果:

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,
  ),
)

在rextester.com上進行演示

這可能不是最有效的方法,但它非常簡短。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM