如何在php中对数组的重复项进行求和

时间:2015-07-05 23:00:28

标签: php arrays

我得到了以下数组。

  [0]=>
    array(7) {
        [0]=>31
        [1]=>14
        [2]=>on
        [3]=>receiver
        [4]=>pollin
        [5]=>0000-e8-de-27-176d10
        [6]=>1
    }
  [1]=>
    array(7) {
        [0]=>31
        [1]=>14
        [2]=>on
        [3]=>receiver
        [4]=>pollin
        [5]=>0000-e8-de-27-176d10
        [6]=>5
    }
  [2]=>
    array(7) {
        [0]=>0
        [1]=>17
        [2]=>"on"
        [3]=>"receiver"
        [4]=>"elro_400"
        [5]=>"0000-e8-de-27-176d10"
        [6]=>1
    }

我试图将[6]的值加在一起,但前提是键0-5的值相同。所以预期的结果是:

  [0]=>
    array(7) {
        [0]=>31
        [1]=>14
        [2]=>on
        [3]=>receiver
        [4]=>pollin
        [5]=>0000-e8-de-27-176d10
        [6]=>6
    }
  [1]=>
    array(7) {
        [0]=>0
        [1]=>17
        [2]=>"on"
        [3]=>"receiver"
        [4]=>"elro_400"
        [5]=>"0000-e8-de-27-176d10"
        [6]=>1
    }

我自己用if(array_key_exists($vals[0]&&$vals[1]&&$vals[2]&&$vals[3]&&$vals[4]&&$vals[5],$res))尝试过,但这并不像我认为的那样有效。

编辑:

如果[0] - [5]相同,我尝试做的就是删除重复项。但是当它删除副本时,它应该总结[6]。

简单示例:

  [0]=>
    array(3) {
        [0]=>31
        [1]=>14
        [2]=>1
    }
  [1]=>
    array(3) {
        [0]=>31
        [1]=>14
        [2]=>4
    }
  [2]=>
    array(3) {
        [0]=>3
        [1]=>18
        [2]=>1
    }

如果[0]和[1]相同,那么它可以很好地删除重复项但是加在一起[2]

这样会导致:

  [0]=>
    array(3) {
        [0]=>31
        [1]=>14
        [2]=>5
    }
  [1]=>
    array(3) {
        [0]=>3
        [1]=>18
        [2]=>1
    }

2 个答案:

答案 0 :(得分:3)

$data = // your dataset above
$result = array();

foreach ($data as $keyA => $valueA){
    foreach ($data as $keyB => $valueB){
        // showing an explicit way of comparing values, eventually you could use php array_diff or array_diff_key functions
        if ($valueA[0] == $valueB[0] &&
            $valueA[1] == $valueB[1] &&
            $valueA[2] == $valueB[2] &&
            $valueA[3] == $valueB[3] &&
            $valueA[4] == $valueB[4] &&
            $valueA[5] == $valueB[5] &&
            $keyA != $keyB){

            $valueA[6] += $valueB[6];
            $result[] = $valueA;
        }
    }
}

var_dump($result); // is now your expected result
如@vonUbisch所述,

更新:,这只会导致重复的项目。非重复项将被忽略。也许你可以解释你的最终目标,这个逻辑似乎很尴尬,也许其他方法可以提供更好的解决方案。

更新2:

似乎仍然是非最佳逻辑,但在这里它

$data = array(
    array( 31, 14, 1),
    array( 31, 14, 4),
    array( 3, 18, 1)
);

$mutant = array();
$result = array();

// deconstruct the data set
foreach ($data as $key => $value){
    // use the first values as an 'id'
    $id = $value[0] . '.' . $value[1];

    // now we use that id as a key in the result array
    if (array_key_exists($id, $mutant)){
        // if the key exists (a previous dataset with same values existed), we add the last item
        $mutant[$id] += $value[2];
    }else{
        // if the key does not exist, we set the last item
        $mutant[$id] = $value[2];
    }
}

var_dump($mutant);

// array (size=2)
//  '31.14' => int 5
//  '3.18' => int 1

// now we rebuild it
foreach ($mutant as $key => $value){
    // split the key into original values
    $items = explode('.', $key);
    // depending on your data, you might need to iterate over $items here to cast them as ints
    // push the last value 
    $items[] = $value;
    // push sub array into result set
    $result[] = $items;
}

var_dump($result);
// array (size=2)
//   0 => 
//     array (size=3)
//       0 => string '31' * note that value is a string!
//       1 => string '14' (length=2)
//       2 => int 5
//   1 => 
//     array (size=3)
//       0 => string '3' (length=1)
//       1 => string '18' (length=2)
//       2 => int 1

答案 1 :(得分:1)

尝试

$array = [
    ...
];

$resultArray = [];
$length = count($array);

for ($i = 0; $i < $length; $i++)
{
    if (!array_key_exists($i, $array))
    {
        continue;
    }

    $array1 = $array[$i];
    $values1 = array_slice($array1, 0, -1);

    for ($j = $i + 1; $j < $length; $j++)
    {
        if (!array_key_exists($j, $array))
        {
            continue;
        }

        $array2 = $array[$j];
        $values2 = array_slice($array2, 0, -1);

        if ($values1 == $values2)
        {
            $array1[6] += $array2[6];

            unset($array[$j]);
        }
    }

    $resultArray[] = $array1;
}

var_dump($resultArray);

$resultArray = [];

for ($i = 0; $i < count($array); $i++)
{
    $array1 = $array[$i];
    $values1 = array_slice($array1, 0, -1);

    $length = count($array);

    for ($j = $i + 1; $j < $length; $j++)
    {
        if (!array_key_exists($j, $array))
        {
            continue;
        }

        $array2 = $array[$j];
        $values2 = array_slice($array2, 0, -1);

        if ($values1 == $values2)
        {
            $array1[6] += $array2[6];

            unset($array[$j]);
        }
    }

    $array = array_values($array);

    $resultArray[] = $array1;
}

var_dump($resultArray);

$resultArray = [];

for ($i = 0; $i < count($array); $i++)
{
    $array1 = $array[$i];
    $values1 = array_slice($array1, 0, -1);

    for ($j = $i + 1; $j < count($array); $j++)
    {
        $array2 = $array[$j];
        $values2 = array_slice($array2, 0, -1);

        if ($values1 == $values2)
        {
            $array1[6] += $array2[6];

            unset($array[$j]);

            $j--;
            $array = array_values($array);
        }
    }

    $resultArray[] = $array1;
}

var_dump($resultArray);

第一个解决方案应该是最快的

我还检查isset($array[$i])的工作速度比arary_key_exists($i, $array)

快得多