I'm trying to reduce/merge an array looking like this
$test = Array
(
[0] => Array
(
[id] => 150023
[rowtext] => part 1
[rowpartnumber] => 4360
[qty] => 2.00
[min_delper] => 2021-07-28
)
[1] => Array
(
[id] => 150026
[rowtext] => part 2
[rowpartnumber] => 4360
[qty] => 2.00
[min_delper] => 2021-07-29
)
[2] => Array
(
[id] => 150023
[rowtext] => part 3
[rowpartnumber] => 4361
[qty] => 4.00
[min_delper] => 2021-08-22
)
[3] => Array
(
[id] => 150023
[rowtext] => part 4
[rowpartnumber] => 4362
[qty] => 2.00
[min_delper] => 2021-09-22
)
);
I want to sum the [qty] where [rowpartnumber] are the same, I can achieve this with
$summa_test = array_reduce($test, function ($a, $b) {
isset($a[$b['rowpartnumber']]) ? $a[$b['rowpartnumber']]['qty'] += $b['qty'] : $a[$b['rowpartnumber']] = $b;
return $a;
});
But I would also like to take the [id] where [rowpartnumer] are the same and add it to [id] so the output should look like this:
Array
(
[0] => Array
(
[id] => 150023 150026
[rowtext] => part 1
[rowpartnumber] => 4360
[qty] => 4.00
[min_delper] => 2021-07-28
)
[1] => Array
(
[id] => 150023
[rowtext] => part 3
[rowpartnumber] => 4361
[qty] => 4.00
[min_delper] => 2021-08-22
)
[2] => Array
(
[id] => 150023
[rowtext] => part 4
[rowpartnumber] => 4362
[qty] => 2.00
[min_delper] => 2021-09-22
)
);
Can't seem to get my head around it, so a little help from you experts would be great, thank you