0

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.

8
  • 4
    Please go read How to Ask. We are not here to simply provide code for you, you are supposed to make an effort and try something first. Commented Oct 1, 2020 at 12:23
  • 1
    provide code as @04FS said, you should make effort Commented Oct 1, 2020 at 12:26
  • @04FS Please check. I have added the solution I'm working on. New to PHP. Commented Oct 1, 2020 at 13:36
  • @Jerson Please check. Added my code. Commented Oct 1, 2020 at 13:36
  • “But it doesn't find out the duplicates.” - of course it doesn’t. You are trying to look for element stored in the array under the date value as key - but you never used that as key to begin with, you are just pushing your items to the arrays with a normal, numeric index. Commented Oct 1, 2020 at 13:58

1 Answer 1

2

This works fine

     $json = '[{
        "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
      }
    ]';

    $decode = json_decode($json,true);

    $res  = array();

    foreach($decode as $key => $vals){
        if(($index = array_search($vals['date'], array_column( $res, 'date'))) !== false) {
            $res[$index]['amount'] += $vals['amount'];
            $res[$index]['date'] = $vals['date'];
        } else {
            $res[] = $vals;
        }
    }

    echo json_encode($res,JSON_PRETTY_PRINT);

Output with :

    [
        {
            "date": "2020 - 09 - 28",
            "amount": 139.9
        },
        {
            "date": "2020 - 10 - 01",
            "amount": 459.85
        }
    ]
Sign up to request clarification or add additional context in comments.

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.