简体   繁体   English

PHP合并具有相同ID的数组

[英]PHP Combine arrays with the same ID

I have $row which outputs the below.我有 $row 输出以下内容。 Array 0 - 4 do not change per user but the last items in each array need to be added together.数组 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,
)

I need to combine them so they are like this:我需要把它们结合起来,所以它们是这样的:

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
)

My code so far is:到目前为止我的代码是:

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

But this is not doing what I expect.但这并不是我所期望的。 Not really sure where to go with this so any help would be much appreciated.不确定该去哪里,所以任何帮助将不胜感激。

Thanks谢谢

You were very close.你非常接近。 You simply needed to add the values.您只需要添加值。

/**
 * @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);

or also (to show the call with a single row):或者(以单行显示呼叫):

$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);

The loop version works as expected with the following data:循环版本使用以下数据按预期工作:

$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,
));

and outputs:和输出:

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
        )

)

This works for me.这对我有用。

$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);

Output with:输出:

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
        )

)

How about that?那个怎么样?

$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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM