按列对二维数组的行进行分组,并对组内的数字列进行求和

问题描述 投票:0回答:3

我有 $row 输出以下内容。数组 0 - 4 不会针对每个用户进行更改,但每个数组中的最后一项需要添加在一起。

array (
  0 => '2',
  1 => 'Joe',
  2 => 'Bloggs',
  3 => '[email protected]',
  4 => '1,2',
  5 => 1,
  6 => 0,
  7 => 1,
)array (
  0 => '2',
  1 => 'Joe',
  2 => 'Bloggs',
  3 => '[email protected]',
  4 => '1,2',
  5 => 0,
  6 => 1,
  7 => 1,
)array (
  0 => '1',
  1 => 'Jane',
  2 => 'Doe',
  3 => '[email protected]',
  4 => '1,4',
  5 => 1,
  6 => 0,
  7 => 1,
)array (
  0 => '1',
  1 => 'Jane',
  2 => 'Doe',
  3 => '[email protected]',
  4 => '1,4',
  5 => 0,
  6 => 0,
  7 => 0,
)

我需要将它们组合起来,所以它们是这样的:

Array
(
    [0] => 2
    [1] => Joe
    [2] => Bloggs
    [3] => [email protected]
    [4] => 1,2
    [5] => 1
    [6] => 1
    [7] => 2
)
Array
(
    [0] => 1
    [1] => Jane
    [2] => Doe
    [3] => [email protected]
    [4] => 1,4
    [5] => 1
    [6] => 0
    [7] => 1
)

到目前为止我的代码是:

$combined = array();
foreach ($row as $item) {
    if (!array_key_exists($item[0], $combined)) {
        $combined[$item[0]] = $item;
    } else {
        
        array_push($combined, $item);
    }   
}

但这并没有达到我的预期。

php arrays multidimensional-array sum grouping
3个回答
1
投票

你们非常亲密。您只需添加值即可。

/**
 * @param array $combined       the combined array
 * @param array $item           a single row of data (8-element array)
 *
 * @returns array               the updated combined array
 */
function combineArray(array $combined, array $item) {
    // This is how we know whether the element exists or not
    $key = $item[0];
    if (!array_key_exists($key, $combined)) {
        // This is a NEW item, so just add it to the combined array.
        $combined[$key] = $item;
    } else {
        // This already exists. Modify the required columns.
        $combined[$key][5] += $item[5];
        $combined[$key][6] += $item[6];
        $combined[$key][7] += $item[7];
        /*
           You could also do this automatically from the type of variable, instead of specifying 5, 6 and 7:
           foreach ($item as $i => $value) {
               if (in_array(gettype($value), array('integer', 'float'))) {
                   $combined[$key][$i] += $value;
               }
           }
        */
    }   

    return $combined;
}

$combined = array();

foreach ($row as $item) {
    $combined = combineArray($combined, $item);
}
// Now convert to "true" array. This is VERY IMPORTANT if you want to output
// it to, say, JSON, where [ 0 => 'a', 1 => 'b' ] and [ 0 => 'a', 2 => 'b' ]
// are two different KINDS of object (the first an array, the second a dict)

$combined = array_values($combined);

或者也(用单行显示调用):

$item = array (
  0 => '2',
  1 => 'Joe',
  2 => 'Bloggs',
  3 => '[email protected]',
  4 => '1,2',
  5 => 1,
  6 => 0,
  7 => 1,
);
$combined = combineArray($combined, $item);

循环版本使用以下数据按预期工作:

$row = array(
array (
  0 => '2',
  1 => 'Joe',
  2 => 'Bloggs',
  3 => '[email protected]',
  4 => '1,2',
  5 => 1,
  6 => 0,
  7 => 1,
),array (
  0 => '2',
  1 => 'Joe',
  2 => 'Bloggs',
  3 => '[email protected]',
  4 => '1,2',
  5 => 0,
  6 => 1,
  7 => 1,
),array (
  0 => '1',
  1 => 'Jane',
  2 => 'Doe',
  3 => '[email protected]',
  4 => '1,4',
  5 => 1,
  6 => 0,
  7 => 1,
),array (
  0 => '1',
  1 => 'Jane',
  2 => 'Doe',
  3 => '[email protected]',
  4 => '1,4',
  5 => 0,
  6 => 0,
  7 => 0,
));

和输出:

Array
(
    [0] => Array
        (
            [0] => 2
            [1] => Joe
            [2] => Bloggs
            [3] => [email protected]
            [4] => 1,2
            [5] => 1
            [6] => 1
            [7] => 2
        )

    [1] => Array
        (
            [0] => 1
            [1] => Jane
            [2] => Doe
            [3] => [email protected]
            [4] => 1,4
            [5] => 1
            [6] => 0
            [7] => 1
        )

)

0
投票

这对我有用。

$res = array();

foreach($array as $vals){
    if(($index = array_search($vals[0], array_column( $res, 0))) !== false) {
        $res[$index][5] += $vals[5];
        $res[$index][6] += $vals[6];
        $res[$index][7] += $vals[7];
    } else {
        $res[] = $vals;
    }
}

print_r($res);

输出:

Array
(
    [0] => Array
        (
            [0] => 2
            [1] => Joe
            [2] => Bloggs
            [3] => [email protected]
            [4] => 1,2
            [5] => 1
            [6] => 1
            [7] => 2
        )

    [1] => Array
        (
            [0] => 1
            [1] => Jane
            [2] => Doe
            [3] => [email protected]
            [4] => 1,4
            [5] => 1
            [6] => 0
            [7] => 1
        )

)

0
投票

那怎么样?

$rows = array(
        array (
            0 => '2',
            1 => 'Joe',
            2 => 'Bloggs',
            3 => '[email protected]',
            4 => '1,2',
            5 => 1,
            6 => 0,
            7 => 1,
        ),
        array (
            0 => '2',
            1 => 'Joe',
            2 => 'Bloggs',
            3 => '[email protected]',
            4 => '1,2',
            5 => 0,
            6 => 1,
            7 => 1,
        ),
        array (
            0 => '1',
            1 => 'Jane',
            2 => 'Doe',
            3 => '[email protected]',
            4 => '1,4',
            5 => 1,
            6 => 0,
            7 => 1,
        ),
        array (
            0 => '1',
            1 => 'Jane',
            2 => 'Doe',
            3 => '[email protected]',
            4 => '1,4',
            5 => 0,
            6 => 0,
            7 => 0,
        ));

foreach ($rows as $row) {

    if (isset($t[$row[3]])) {

        $t[$row[3]][5] += $row[5];
        $t[$row[3]][6] += $row[6];
        $t[$row[3]][7] += $row[7];
    } else 
        $t[$row[3]] = $row;
}

foreach ($t as $r) $combined[] = $r;

print_r($combined);
© www.soinside.com 2019 - 2024. All rights reserved.