如果键值相同,如何添加数组

时间:2016-01-20 11:19:53

标签: php multidimensional-array

我很难跟踪多维数组。

[
    0 => array:7 [
        "date" => "2016-01-19"
        "placement_id" => 1
        "requests" => 18
        "revenue" => 1
    ],
    1 => array:7 [
        "date" => "2016-01-19"
        "placement_id" => 1
        "requests" => 2
        "revenue" => 0.2
    ]
];

如果placement_id相同,我想要结果数组:

        1 => array:7 [
            "date" => "2016-01-19"
            "placement_id" => 1
            "requests" => 20
            "revenue" => 1.2
        ]

2 个答案:

答案 0 :(得分:2)

要求是生成一个具有以下内容的输出数组:

  • 具有相同' placement_id'的项目有这样的要求'和revenue总结。
  • 输出数组中条目的键将是' placement_id'。

这意味着输出数组将小于输入数组。

我决定使用array_reduce功能。没有特殊原因,foreach循环工作正常。它不再有效率。这是完全不同的。

关于array_reduce的重点是$carry(累加器)可以是array ......

Working example at Eval.in

代码:

$outArray = array();

$outArray = array_reduce($src,
                  function($carry, $item)  { // accumulate values if possible

                      $carryKey = $item['placement_id']; // array key

                      if (isset($carry[$carryKey])) { // accumulate values

                          $carry[$carryKey]['requests'] += $item['requests'];
                          $carry[$carryKey]['revenue'] += $item['revenue'];

                      } else { // is new - add to the output...

                          $carry[$carryKey] = $item;
                      }
                      return $carry;
                  },
                  array() /* accumulator ($carry) is an internal variable */);

输出数组:

array (2) [
    '1' => array (4) [
        'date' => string (10) "2016-01-19"
        'placement_id' => integer 1
        'requests' => integer 20
        'revenue' => float 1.2
    ]
    '666' => array (4) [
        'date' => string (10) "2016-04-01"
        'placement_id' => integer 666
        'requests' => integer 266
        'revenue' => float 666.20000000000005
    ]
]

测试数据:

$src = array(
    0 => array(
        "date" => "2016-01-19",
        "placement_id" => 1,
        "requests" => 18,
        "revenue" => 1,
    ),
    1 => array(
        "date" => "2016-04-01",
        "placement_id" => 666,
        "requests" => 266,
        "revenue" => 666.2,
    ),
    2 => array(
        "date" => "2016-01-19",
        "placement_id" => 1,
        "requests" => 2,
        "revenue" => 0.2,
    ),
);

答案 1 :(得分:1)

$arr参数作为您显示的数组,我们可以创建一个这样的函数来查找重复ID并聚合它们。 $output数组将返回结果。

public function checkArray($arr) {

    $output = array();
    $deleted = array();
    foreach($arr as $key => $value){
        if (!in_array($key, $deleted)) {
            $entry = array();
            $entry['date'] = $value['date'];
            $entry['placement_id'] = $value['placement_id'];
            $entry['requests'] = $value['requests'];
            $entry['revenue'] = $value['revenue'];
            foreach($arr as $key2 => $value2){
                if($key != $key2 && $value['placement_id'] == $value2['placement_id']){
                    $entry['requests'] += $value2['requests'];
                    $entry['revenue'] += $value2['revenue'];
                    $deleted[] = $key2;
                }
            }
            $output[] = $entry;
         }
    }
    return $output;
}