簡體   English   中英

獲取嵌套數組php中具有相同id的對象的總和

[英]Get sum of objects with same id inside nested array php

這是我在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中實現此目標,而又沒有任何性能問題?

您可以通過將PHP函數用作array-mergearray-columnarray-reduce來實現

讓我們將其分為三步:

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);

這將為您提供所需的輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM