0

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer, it's not using array_map, you mean replace current array_merge with array_map
0

We need to increase Payment Method instead of merge array, to prevent overwrite old value. Below code working for me

<?php
$shipment = [
    [
        'Datum' => '2016-07-07',
        'C' => 1,
    ],
    [
        'Datum' => '2016-07-07',
        'C' => 1,
    ],
    [
        'Datum' => '2016-07-07',
        'Paypal' => 1,
    ],
    [
        'Datum' => '2016-07-07',
        'Bank' => 1,
    ],
    [
        'Datum' => '2016-07-12',
        'Bank' => 1,
    ],
];

$template = ['C' => 0, 'Afterpay' => 0, 'Paypal' => 0, 'Bank' => 0, 'Ideal' => 0];
$result = [];

array_walk($shipment, function ($v) use (&$result, $template) {
    // Init by Datum if it is not in array
    $date = $v['Datum'];
    if (!isset($result[$date])) {
        $result[$date] = $template;
    }

    // Unset Datum to get final payment method only
    unset($v['Datum']);
    $itemKey = array_keys($v)[0];

    // Increase number
    $result[$date][$itemKey] += $v[$itemKey];
});

// Dump result
var_dump($result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.