I have array like as follow,
Array(
[0] => Array(
[account] => 251234567890,
[price] => 83
) [1] => Array(
[account] => 251234567890,
[price] => 27
) [2] => Array(
[account] => 251234564526,
[price] => 180
) [3] => Array(
[account] => 251234567890,
[price] => 40
)
)
now with i want to merge array with same account
and sum of it's particular price
.
I want output array like this,
Array(
[251234567890] => Array(
[account] => 251234567890,
[price] => 150
) [251234564526] => Array(
[account] => 251234564526,
[price] => 180
)
)
I have tried like this,
$res = array();
$k = 0;
foreach ($to_account as $vals) {
if (array_key_exists($vals['account'], $res)) {
$res[$k]['price'] += $vals['price'];
$res[$k]['account'] = $vals['account'];
$k++;
} else {
$res[$k] = $vals;
$k++;
}
}
As here in input array only two unique account
is present so output array should be of that two account with sum of it's price
I have seen something like this in python
from here but it want be helpful as it is in python i want it in php
i hope someone can help me in this
Thank you