i have this tables in my mysql database:
+------------+-----------------------+--------+
| 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 :
$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.
the php object result is :
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=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? thanks.
You are reusing $payment_methods_array
again and again. You need to reset it's values to 0
for every day. 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,
),
)
This might be not the most effitient way, but it's quite short and simple.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.