I'm wring a PHP script to group some payment info using date. part of original array is like this.
array(50) {
[0]=>
array(2) {
["Datum"]=>
string(10) "2016-07-07"
["C"]=>
int(1)
}
[1]=>
array(2) {
["Datum"]=>
string(10) "2016-07-07"
["C"]=>
int(1)
}
[2]=>
array(2) {
["Datum"]=>
string(10) "2016-07-07"
["Paypal"]=>
int(1)
}
[3]=>
array(2) {
["Datum"]=>
string(10) "2016-07-07"
["Bank"]=>
int(1)
}
[4]=>
array(2) {
["Datum"]=>
string(10) "2016-07-12"
["C"]=>
int(1)
}
[5]=>
array(2) {
["Datum"]=>
string(10) "2016-06-13"
["C"]=>
int(1)
}
[6]=>
array(2) {
["Datum"]=>
string(10) "2016-06-13"
["C"]=>
int(1)
}
[7]=>
array(2) {
["Datum"]=>
string(10) "2016-06-13"
["Afterpay"]=>
int(1)
}
[8]=>
array(2) {
["Datum"]=>
string(10) "2016-06-13"
["C"]=>
int(1)
}
[9]=>
array(2) {
["Datum"]=>
string(10) "2016-06-13"
["C"]=>
int(1)
}
[10]=>
array(2) {
["Datum"]=>
string(10) "2016-06-12"
["C"]=>
int(1)
}
[11]=>
array(2) {
["Datum"]=>
string(10) "2016-06-13"
["C"]=>
int(1)
}
[12]=>
array(2) {
["Datum"]=>
string(10) "2016-06-13"
["C"]=>
int(1)
}
[13]=>
array(2) {
["Datum"]=>
string(10) "2016-06-13"
["C"]=>
int(1)
}
[14]=>
array(2) {
["Datum"]=>
string(10) "2016-06-13"
["C"]=>
int(1)
}
[15]=>
array(2) {
["Datum"]=>
string(10) "2016-06-13"
["C"]=>
int(1)
}
[16]=>
array(2) {
["Datum"]=>
string(10) "2016-06-13"
["C"]=>
int(1)
}
[17]=>
array(2) {
["Datum"]=>
string(10) "2016-06-13"
["C"]=>
int(1)
}
[18]=>
array(2) {
["Datum"]=>
string(10) "2016-06-14"
["Afterpay"]=>
int(1)
}
[19]=>
array(2) {
["Datum"]=>
string(10) "2016-06-14"
["C"]=>
int(1)
}
}
I wrote some codes to group those by date. here is my code
//$shipment - this is the original array
$array1 = array('C' => null, 'afterpay' => null,'paypal' => null, 'Bank' => null, 'Ideal' => null);
$array2 = array();
array_walk($shipment, function ($v) use (&$array2, $array1) {
$a = $v['Datum'];
if (!isset($array2[$a])) {
$array2[$a] = $array1;
}
unset($v['Datum']);
$array2[$a] = array_merge($array2[$a], $v);
});
Output after grouping
Array
(
[2016-07-07] => Array
(
[C] => 1
[afterpay] =>
[paypal] =>
[Bank] => 1
[Ideal] =>
[Paypal] => 1
)
[2016-07-12] => Array
(
[C] => 1
[afterpay] =>
[paypal] =>
[Bank] =>
[Ideal] =>
)
[2016-06-13] => Array
(
[C] => 1
[afterpay] =>
[paypal] =>
[Bank] =>
[Ideal] =>
[Afterpay] => 1
)
[2016-06-12] => Array
(
[C] => 1
[afterpay] =>
[paypal] =>
[Bank] =>
[Ideal] =>
)
[2016-06-14] => Array
(
[C] => 1
[afterpay] =>
[paypal] =>
[Bank] =>
[Ideal] =>
[Afterpay] => 1
)
)
but it's difficult me to find a way to count the payment method numbers and assign it
Example for 2016-07-07 array should be C= 2,Paypal = 1, Bank = 1
[2016-07-07] => Array
(
[C] => 2
[afterpay] =>
[paypal] =>
[Bank] => 1
[Ideal] =>
[Paypal] => 1
)
can someone help me to add some code for get sum of payment methods and assign, thanks a lot
This happens because array_merge
override the data if 2 keys are the same - what you need is to add all fields.
First, change $array1
with init field as 0
instead of null
.
Second, in your array_walk
, replace $array2[$a] = array_merge($array2[$a], $v);
with:
foreach($v as $k => $v)
$array2[$a][$k] += $v;
This way you will add up each category