Php 如何检查JSON对象数组中的相同值并将它们组合在一起?

Php 如何检查JSON对象数组中的相同值并将它们组合在一起?,php,arrays,json,laravel,Php,Arrays,Json,Laravel,我有这个JSON对象数组。我需要检查相同的日期并合并金额。我需要用PHP来做这件事。(我用的是拉威尔) 所以输出应该是这样的: [ {date : 2020-09-28, amount : 139.9} {date : 2020-10-01, amount : 459.85} ] 请问,实现这一点的最佳方式是什么 这是我尝试过的解决方案: $new_values = array(); foreach ($orders as $order) {

我有这个JSON对象数组。我需要检查相同的日期并合并金额。我需要用PHP来做这件事。(我用的是拉威尔)

所以输出应该是这样的:

[
   {date : 2020-09-28, amount : 139.9}
   {date : 2020-10-01, amount : 459.85}
]
请问,实现这一点的最佳方式是什么

这是我尝试过的解决方案:

$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);
$new_values=array();
foreach($orders作为$order){
$order\u total=$order->order\u checkout->shoppingcart->getTotalPrice();
$order_date=$order->created_at->格式('Y-m-d');
打印(订单日期);
打印(“-”);
打印(订单总额);
打印“
”; 如果(数组\键\存在($order\ U date,$new\ U values)){ echo$order_date.“found”; }否则{ 回声“未找到”; } 数组推送($new_值,数组($order_date=>$order_total)); } 打印(
); 打印(新值);
但是它找不到重复项。

这很好

     $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);
输出包括:

    [
        {
            "date": "2020 - 09 - 28",
            "amount": 139.9
        },
        {
            "date": "2020 - 10 - 01",
            "amount": 459.85
        }
    ]
这个很好用

     $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);
输出包括:

    [
        {
            "date": "2020 - 09 - 28",
            "amount": 139.9
        },
        {
            "date": "2020 - 10 - 01",
            "amount": 459.85
        }
    ]

请去读。我们在这里不是简单地为您提供代码,您应该付出努力并首先尝试一些东西effort@04FS请查收。我已经添加了我正在研究的解决方案。PHP新手。@Jerson请检查。添加了我的代码。“但是它没有找到重复的。”-当然没有。您正在尝试查找存储在数组中日期值下的元素作为键-但您从未使用该键作为开始,您只是使用普通的数字索引将项目推送到数组中。请阅读。我们在这里不是简单地为您提供代码,您应该付出努力并首先尝试一些东西effort@04FS请查收。我已经添加了我正在研究的解决方案。PHP新手。@Jerson请检查。添加了我的代码。“但是它没有找到重复的。”-当然没有。您试图查找存储在数组中作为键的日期值下的元素,但您从未使用该键作为开始,您只是使用普通的数字索引将项目推送到数组中。