PHP合并具有相同ID的数组

Fazberry

我有$ 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);
    }   
}

但这没有达到我的期望。不太确定如何处理此问题,因此不胜感激。

谢谢

塞尔尼

你很亲近 您只需要添加值即可。

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

)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章