Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中添加匹配的数组键参数_Php_Arrays - Fatal编程技术网

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

在PHP中添加匹配的数组键参数,php,arrays,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] =&g

我有以下数组,在这个数组中,键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键值(即日期)相同的数组


我有成千上万张这样的唱片。所以请建议简单快捷的方法。我还需要保留所有内部数组的键。

您需要使用
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));

我将操纵数组,使112键的值成为新数组的键,该值是原始数组中与112值匹配的每个项的列表。然后对其进行迭代,检查数组中是否有多个项,如果有,则求和。即

$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

我没有测试,但应该可以工作

您需要保持原始结构吗?是的…与我给出的结果数组相同更新了我的原始答案,注意到一些拼写错误