PHP合并具有相同ID的数组

时间:2020-11-11 13:26:16

标签: php

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

array (
  0 => '2',
  1 => 'Joe',
  2 => 'Bloggs',
  3 => 'joe.bloggs@mail.com',
  4 => '1,2',
  5 => 1,
  6 => 0,
  7 => 1,
)array (
  0 => '2',
  1 => 'Joe',
  2 => 'Bloggs',
  3 => 'joe.bloggs@mail.com',
  4 => '1,2',
  5 => 0,
  6 => 1,
  7 => 1,
)array (
  0 => '1',
  1 => 'Jane',
  2 => 'Doe',
  3 => 'jane.doe@mail.com',
  4 => '1,4',
  5 => 1,
  6 => 0,
  7 => 1,
)array (
  0 => '1',
  1 => 'Jane',
  2 => 'Doe',
  3 => 'jane.doe@mail.com',
  4 => '1,4',
  5 => 0,
  6 => 0,
  7 => 0,
)

我需要将它们组合在一起,所以它们就像这样:

Array
(
    [0] => 2
    [1] => Joe
    [2] => Bloggs
    [3] => joe.bloggs@mail.com
    [4] => 1,2
    [5] => 1
    [6] => 1
    [7] => 2
)
Array
(
    [0] => 1
    [1] => Jane
    [2] => Doe
    [3] => jane.doe@mail.com
    [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);
    }   
}

但是,这没有达到我的期望。不太确定该怎么做,因此不胜感激。

谢谢

3 个答案:

答案 0 :(得分: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 => 'joe.bloggs@mail.com',
  4 => '1,2',
  5 => 1,
  6 => 0,
  7 => 1,
);
$combined = combineArray($combined, $item);

循环版本可以正常使用以下数据:

$row = array(
array (
  0 => '2',
  1 => 'Joe',
  2 => 'Bloggs',
  3 => 'joe.bloggs@mail.com',
  4 => '1,2',
  5 => 1,
  6 => 0,
  7 => 1,
),array (
  0 => '2',
  1 => 'Joe',
  2 => 'Bloggs',
  3 => 'joe.bloggs@mail.com',
  4 => '1,2',
  5 => 0,
  6 => 1,
  7 => 1,
),array (
  0 => '1',
  1 => 'Jane',
  2 => 'Doe',
  3 => 'jane.doe@mail.com',
  4 => '1,4',
  5 => 1,
  6 => 0,
  7 => 1,
),array (
  0 => '1',
  1 => 'Jane',
  2 => 'Doe',
  3 => 'jane.doe@mail.com',
  4 => '1,4',
  5 => 0,
  6 => 0,
  7 => 0,
));

并输出:

Array
(
    [0] => Array
        (
            [0] => 2
            [1] => Joe
            [2] => Bloggs
            [3] => joe.bloggs@mail.com
            [4] => 1,2
            [5] => 1
            [6] => 1
            [7] => 2
        )

    [1] => Array
        (
            [0] => 1
            [1] => Jane
            [2] => Doe
            [3] => jane.doe@mail.com
            [4] => 1,4
            [5] => 1
            [6] => 0
            [7] => 1
        )

)

答案 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] => joe.bloggs@mail.com
            [4] => 1,2
            [5] => 1
            [6] => 1
            [7] => 2
        )

    [1] => Array
        (
            [0] => 1
            [1] => Jane
            [2] => Doe
            [3] => jane.doe@mail.com
            [4] => 1,4
            [5] => 1
            [6] => 0
            [7] => 1
        )

)

答案 2 :(得分:0)

那怎么样?

$rows = array(
        array (
            0 => '2',
            1 => 'Joe',
            2 => 'Bloggs',
            3 => 'joe.bloggs@mail.com',
            4 => '1,2',
            5 => 1,
            6 => 0,
            7 => 1,
        ),
        array (
            0 => '2',
            1 => 'Joe',
            2 => 'Bloggs',
            3 => 'joe.bloggs@mail.com',
            4 => '1,2',
            5 => 0,
            6 => 1,
            7 => 1,
        ),
        array (
            0 => '1',
            1 => 'Jane',
            2 => 'Doe',
            3 => 'jane.doe@mail.com',
            4 => '1,4',
            5 => 1,
            6 => 0,
            7 => 1,
        ),
        array (
            0 => '1',
            1 => 'Jane',
            2 => 'Doe',
            3 => 'jane.doe@mail.com',
            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);