在PHP中添加匹配的数组键参数

时间:2015-04-22 19:29:10

标签: php arrays

我有以下数组,在此数组中,键112的日期相同。 我需要添加那些日期相同的数组。

Array
(
[0] => Array
    (
        [112] => 2015-02-17
        [97] => 20.00
        [84] => 13.00
    )

[1] => Array
    (
        [112] => 2015-02-17
        [97] => -5
        [84] => 0
    )

[2] => Array
    (
        [112] => 2015-02-17
        [97] => -5
        [84] => 0
    )

[3] => Array
    (
        [112] => 2015-03-17
        [97] => 10
        [84] => 0
    )

[4] => Array
    (
        [112] => 2015-03-17
        [97] => -5
        [84] => 0
    )

[5] => Array
    (
        [112] => 2015-03-17
        [97] => -5
        [84] => 0
    )

[6] => Array
    (
        [112] => 2015-01-17
        [97] => 2
        [84] => 0
    )

[7] => Array
    (
        [112] => 2015-01-17
        [97] => 13
        [84] => 0
    )

[8] => Array
    (
        [112] => 2015-01-17
        [97] => 2
        [84] => 0
    )
)

例如,从上面的数组我需要以下结果:

Array
(
[0] => Array
    (
        [112] => 2015-02-17
        [97] => 10.00
        [84] => 13.00
    )

[1] => Array
    (
        [112] => 2015-03-17
        [97] => 0
        [84] => 0
    )

[2] => Array
    (
        [112] => 2015-01-17
        [97] => 17
        [84] => 0
    )
)

在这个结果数组中,我需要添加那些其日期的112键值相同的数组。

我有成千上万的这样的记录。所以请建议简单快捷的方式。此外,我需要保留所有内部数组的键。

2 个答案:

答案 0 :(得分:1)

你需要使用foreach循环并迭代数组中的每个元素。

$array = array(
  array(
    112 => '2015-02-17',
    97 => 20,
    84 => 13
  ),
  array(
    112 => '2015-02-17',
    97 => -5,
    84 => 13
  ),
  array(
    112 => '2015-02-17',
    97 => -5,
    84 => 13
  ),
  array(
    112 => '2015-02-18',
    97 => 10,
    84 => 13
  ),
  array(
    112 => '2015-02-18',
    97 => 10,
    84 => 13
  ),
);


$result = array();
foreach ($array as $item) {
  if (isset($result[$item[112]])) {
    foreach ($item as $key => $value) {
      if ($key != 112) { # we don't need to sum 'date column' 
        if (isset($result[$item[112]][$key])) {
          $result[$item[112]][$key] += $value;
        } else {
          $result[$item[112]][$key] = $value;
        }
      }
    }
  } else {
    $result[$item[112]] = $item;
  }
}

print_r(array_values($result));

答案 1 :(得分:1)

我会操纵数组,因此112键的值是新数组的键,值是原始中与112值匹配的每个项的列表。然后迭代它检查数组中是否有多于1个项目,如果是,则将它们相加。即

$data = array(...); // all your source data
$matches = array();
$results = array();

// reformat data into an associative array
foreach($data as $item) {
    if(!array_key_exists($item[112], $matches)} { 
        $matches[$item[112]] = array();
    }
    $matches[$item[112]][] = $item;
}

foreach($matches as $items) {
    if(count($items) == 1)
        continue; // if we don't have at least 2 matching 112's, skip
    $sum = array();
    foreach($items as $item)
    {
        // iterate over each key, and add it to the array
        foreach($item as $key => $value)
        {
            if($key == 112)
                $sum[$key] = $value; // assign 112, don't add it again

            if(array_key_exists($key, $sum))
                $sum[$key] += $value;
            else
                $sum[$key] = $value;
        }
    }
    $results[] = $sum; // add sum to list of sum'ed items
}
// $results now contains any matches sum'ed together

我没有测试,但应该工作