0

I have already seen this stackoverflow page but it is not helping me.

I want to group by two columns and sum the values of a third column.

If the discount_id and dis_percent are the same then add the discount_value.

Here is my array:

$dis = [
    [['Dis_id' => 'Dl-Dis1'], ['Dis_per' => '7.500'], ['Dis_val' => '192.75']],
    [['Dis_id' => 'Dl-Dis2'], ['Dis_per' => '2.500'], ['Dis_val' => '97.88']],
    [['Dis_id' => 'Dl-Dis1'], ['Dis_per' => '5.000'], ['Dis_val' => '39.90']],
    [['Dis_id' => 'Dl-Dis2'], ['Dis_per' => '2.500'], ['Dis_val' => '99.90']]
];

The output that I need is:

D1-Dis1->7.5->192.75
D1-Dis1->5.0->39.9
D1-Dis2->2.5->197.78

My code looks like this:

$newarr = array();
$reverse_map = array();

foreach($dis as $idx => $entry) {
    if (isset($reverse_map[$entry['Dis_id']])) {
         // have we seen this name before? retrieve its original index value
         $idx = $reverse_map[$entry['Dis_id']]; 
    } else {
         // nope, new name, so store its index value
         $reverse_map[$entry['Dis_id']] = $idx;
    }

    // copy the 'constant' values
    $newarr[$idx]['Dis_id'] = $entry['Dis_id'];
    $newarr[$idx]['Dis_per'] = $entry['Dis_per'];

    // sum the qtd_post values to whatever we previously stored.        
    foreach($entry['Dis_val'] as $x => $y) {
        $newarr[$idx]['Dis_val'][$x] += $y;
    }
}
7
  • If the dis_id and dis_per are what? Also, have you got any examples of code where you've tried to solve this so we can see where you are having issues? Commented Jun 4, 2014 at 8:51
  • dis_id is the name and dis_per is the percentage for the dis_id. i gave the amt and the result after deduct from the dicount percent is dis_val.. please see the code i tried Commented Jun 4, 2014 at 9:16
  • 1
    "a condition if the discount_id and dis_percent are then add the discount_value" <- if they are what? You haven't said what the condition is Commented Jun 4, 2014 at 9:26
  • sql query can do it. You need to provide it Commented Jun 4, 2014 at 9:34
  • 1
    Right.. So I'm getting the idea that you want to sum up the Dis_val for all entries, grouped by Dis_id and Dis_per. I've written a solution, but also noticed that your array structure is quite odd, and doesn't match how your attempted solution works. Is it intentional that each entry contains a subarray, in which there is just a single element for either Dis_id, Dis_per or Dis_val? Hopefully you'll get what I mean by this; pastebin.com/FDjVWy6z Commented Jun 4, 2014 at 10:57

2 Answers 2

2

This is the solution I've come up with based off of the understanding that your intended array structure was as so;

$dis = array(
    array(
        'Dis_id'  => 'Dl-Dis1',
        'Dis_per' => 7.500,
        'Dis_val' => 192.75
    ),
    ...
);

It determines the solution by creating a multidimensional array where the first dimension is the Dis_id, and the second dimension is the Dis_per, and the value becomes the sum of the Dis_val;

$sums = array();

foreach ($dis as $entry) {
    if (!isset($sums[$entry['Dis_id']])) {
        $sums[$entry['Dis_id']] = array();
    }
    if (!isset($sums[$entry['Dis_id']]["{$entry['Dis_per']}"])) {
        $sums[$entry['Dis_id']]["{$entry['Dis_per']}"] = 0;
    }
    $sums[$entry['Dis_id']]["{$entry['Dis_per']}"] += $entry['Dis_val'];
}

See this working example; https://eval.in/158661

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

Comments

0

As you iterate your input array, use array destructuring to isolate the Dis_id and Dis_per values and use them as keys when storing your Dis_val.

If the key path hasn't been encountered before, default the stored amount to 0 and add the current amount to it.

Code: (Demo)

$result = [];
foreach ($dis as [['Dis_id' => $id], ['Dis_per' => $per], ['Dis_val' => $val]]) {
    $result[$id][$per] = ($result[$id][$per] ?? 0) + $val;
}
var_export($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.