首页 文章

在嵌套数组php中获取具有相同id的对象的总和

提问于
浏览 1684
0

这是我在php中的对象数组:

Array( [0]=>
  Array (
    [added_time] => 2018-12-09 14:00:00+00 
    [id] => 123
    [places] => 
      [{
        "place_id" : 1,
        "total" : 5, 
        "empty" : 0,
        "weight" : 68000,
        "persons" : 
         [{
           "person_id" : 2,
           "person_name" : "ABC",
           "total":100
          }, 
          {
           "person_id" : 3
           "person_name" : "DEF",
           "total":200
         }]
      },
        "place_id" : 4,
        "total" : 10, 
        "empty" : 0,
        "weight" : 54000,
        "persons" : 
          [{
            "person_id" : 2,
            "person_name" : "ABC"
            "total":100
           }, 
           {
            "person_id" : 6
            "person_name" : "GHI",
           }
         ]
     ],
  ),
   Array (
    [added_time] => 2018-12-09 15:00:00+00 
    [id] => 456
    [places] => 
      [{
        "place_id" : 1,
        "total" : 5, 
        "empty" : 0,
        "weight" : 68000,
        "persons" : 
         [{
           "person_id" : 2,
           "person_name" : "ABC",
           "total":200
          }, 
          {
           "person_id" : 3
           "person_name" : "DEF",
           "total":300
         }]
      }]
   )
)

我试图在person_id的基础上得到总和,如person_id分组 .

我想要的结果应该是:

Array (
 [added_time] => 2018-12-09 14:00:00+00 
 [id] => 123
 persons:array(
   Array([0]=>
     [person_id] : 2,
     [person_name] : "ABC",
     [total]:200
   ),
   Array([1]=>
     [person_id] : 3,
     [person_name] : "DEF",
     [total]:300
   ),
   Array([2]=>
     [person_id] : 6,
     [person_name] : "GHI",
     [total]:500
    )
  ),
 [added_time] => 2018-12-09 15:00:00+00 
 [id] => 123
 persons:array(
   Array([0]=>
     [person_id] : 2,
     [person_name] : "ABC",
     [total]:200
   ),
   Array([1]=>
     [person_id] : 3,
     [person_name] : "DEF",
     [total]:300
    )
  )
)

我怎样才能达到这个效果 . 我可以使用 foreach 但我不想因为三次迭代而使用它 .

有什么其他方法可以在PHP中实现这一点,没有任何性能问题?

1 回答

  • 2

    您可以通过使用PHP函数array-mergearray-columnarray-reduce来实现 .

    让我们分为3步:

    1.给出所有人名单的总数 . 定义以下reduce函数:

    function reduce($carry, $item)
    {
        if (!in_array($item["person_name"], array_column($carry, "person_name")))
            $carry[] = $item;
        else {
            foreach($carry as &$person) {
                    if ($item["person_name"] == $person["person_name"])
                            $person["total"] += $item["total"];
            }
        }
        return $carry;
    }
    

    2.对于数组中的每个元素,使用步骤1中的 reduce 创建输出:

    function reduceElement($arr) {
            $res = array("added_time" => $arr["time"], "id" => $arr["id"]);
            $persons = array();
            foreach($arr["places"] as $place) {
                    $persons = array_merge($persons, $place["persons"]);
            }
            $res["persons"] = array_reduce($persons, "reduce", array());
            return $res;
    }
    

    3.结合一切:

    $elemA= array("id"=>1, "time"=>"2018", "places" => array(array("persons" => array(array("person_name" => "ABC", "total" => 100), array("person_name" => "DEF", "total" => 200))), array("persons" => array(array("person_name" => "ABC", "total" => 100), array("person_name" => "GHI", "total" => 100)))));
    $elemB = array("id"=>2, "time"=>"2017", "places" => array(array("persons" => array(array("person_name" => "ABC", "total" => 200), array("person_name" => "DEF", "total" => 300)))));
    $arr = array($elemA, $elemB);
    $res = array();
    foreach($arr as $obj)
        $res[] = reduceElement($obj);
    
    print_r($res);
    

    这将为您提供要求

相关问题