I have this JSON object array. I need to check for same dates and combine the amount. I need to do this in PHP. (I'm using Laravel)
[
{date : 2020-09-28, amount : 69.95}
{date : 2020-09-28, amount : 69.95}
{date : 2020-10-01, amount : 69.95}
{date : 2020-10-01, amount : 69.95}
{date : 2020-10-01, amount : 319.95}
]
So the output should be like this:
[
{date : 2020-09-28, amount : 139.9}
{date : 2020-10-01, amount : 459.85}
]
What is the best way to implement this please?
This is the solution I have tried:
$new_values = array();
foreach ($orders as $order) {
$order_total = $order->order_checkout->shoppingcart->getTotalPrice();
$order_date = $order->created_at->format('Y-m-d');
print_r($order_date);
print(" - ");
print_r($order_total);
print "<br/>";
if (array_key_exists($order_date, $new_values)) {
echo $order_date . " found";
} else {
echo "Not found";
}
array_push($new_values, array($order_date => $order_total));
}
print("<br>");
print_r($new_values);
But it doesn't find out the duplicates.