Home > database >  PHP array sum and grouping duplicate
PHP array sum and grouping duplicate

Time:11-16

I'm having this array:

$statuses = ['PROSPECT','BACKLOG','PROSPECT'];
$of_tranxs = [2,1,2];
$revs = [3,1,3];
$mgps = [4,1,4];

And I want sum by duplicate status. output an array like this:

array(
  'status' => ['PROSPECT','BACKLOG'],
  'of_tranx' => [4,1],
  'rev' => [6,1],
  'mgp' => [8,1]
)

I'm Using PHP 7.4

I have tried with the following:

$result = array_zip_combine(
        ['status', 'of_tranx', 'rev', 'mgp'], 
        $statuses, $of_tranxs, $revs, $mgps
    );

then I foreach but the result is not what I want.

CodePudding user response:

I would go by making a dictionary / lookup table / hash table (this goes by many names) and sum only numeric numbers, while stripping other duplicates for any other type.

So I would write a helper function like this:

function sumDuplicatesInArray($array) {
    $dict = [];
    foreach($array as $item) {
        $dict[$item] = is_numeric($item) ? ($dict[$item] ?? 0)   $item : $item;
    }
    return array_values($dict);
} 

And apply it on the arrays you need. See http://sandbox.onlinephpfunctions.com/code/c5ea405718ba7d6b7fdfcae2271964a55d85b71b

CodePudding user response:

$array = array(
    'status' => ['PROSPECT','BACKLOG','PROSPECT'],
    'of_tranx' => [2,1,2],
    'rev' => [3,1,3],
    'mgp' => [4,1,4]
);

function sumDuplicatesInArray($array) {
    $dict = [];
    foreach($array as $item) {
        $dict[$item] = is_numeric($item) ? ($dict[$item] ?? 0)   $item : $item;
    }
    return array_values($dict);
}
$final_array = [];
$final_array['status'][]  = sumDuplicatesInArray($array['status']); 
$final_array['of_tranx'][]  = sumDuplicatesInArray($array['of_tranx']); 
$final_array['rev'][]  = sumDuplicatesInArray($array['rev']); 
$final_array['mgp'][]  = sumDuplicatesInArray($array['mgp']); 
print_R($final_array); 

CodePudding user response:

Assuming that you mean to take the unique values of status and then add the corresponding items in the other arrays...

This first gets the unique values for the status and (for convenience) adds all of the other arrays to a single array to allow a foreach.

Then looping over the original statuses, it adds the values to a new array (created with array_fill_keys()) which acts to add all of the values for that type together. Using array_values() to remove the index keys...

$statuses = ['PROSPECT', 'BACKLOG', 'PROSPECT'];
$of_tranxs = [2, 1, 2];
$revs = [3, 1, 3];
$mgps = [4, 1, 4];

$statusUnique = array_unique($statuses);
$arrays = [$of_tranxs, $revs, $mgps];
foreach ($arrays as $index => $array) {
    $out = array_fill_keys($statusUnique, 0);
    foreach ($statuses as $ind => $stat) {
        $out[$stat]  = $array[$ind];
    }
    $arrays[$index] = array_values($out);
}
print_r(array_values($statusUnique));
print_r($arrays);

giving...

Array
(
    [0] => PROSPECT
    [1] => BACKLOG
)
Array
(
    [0] => Array
        (
            [0] => 4
            [1] => 1
        )

    [1] => Array
        (
            [0] => 6
            [1] => 1
        )

    [2] => Array
        (
            [0] => 8
            [1] => 1
        )

)
  •  Tags:  
  • php
  • Related