在嵌套数组php

时间:2018-12-10 09:19:54

标签: php arrays multidimensional-array

这是我在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为基础来求和,例如group by 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 个答案:

答案 0 :(得分: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);

这将为您提供所需的投放量