【问题标题】:PHP Combine arrays with the same IDPHP 合并具有相同 ID 的数组
【发布时间】:2020-11-11 13:26:16
【问题描述】:

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

但这并没有达到我的预期。不确定该去哪里,所以任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 这能回答你的问题吗? PHP get the last 3 elements of an array
  • @BurhanKashour 这不是 OP 所要求的。
  • @Fazberry 您的解决方案甚至没有尝试总结这些值,您只是不断用最新的替换项目。此外,当您执行array_push 时,该元素将插入到下一个可用索引处。您的检查基于与用户 ID 相同的索引。如果你想那样做,那么废弃推送并在所需的索引处手动插入(基本上,你的if 分支内容应该在else 分支中,if 分支应该做添加)。
  • @FazBerry 你能把你的数组粘贴到这里而不是打印出来的,这样我们就可以轻松插入它
  • @jerson 不太清楚你的意思。

标签: php


【解决方案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
        )

)

【讨论】:

  • 您好,感谢您的意见。这对我不起作用。我遇到了一个致命错误:错误:不能使用带有字符串偏移的分配操作符。我猜它正在尝试添加一个字符串
  • @Fazberry 我的代码与您提供的数据一起正常工作(我已经测试过了)。如果不同的数据产生不同的结果,您应该发布您实际使用的数据。会不会是您使用$row 来表示单个组件数组 而不是$item
【解决方案2】:

这对我有用。

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

)

【讨论】:

    【解决方案3】:

    怎么样?

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

    【讨论】:

    • 问题是电子邮件或第 3 列将是关键
    • ok 然后将其填充到另一个数组中 `foreach ($combined as $r) $without[] = $r;
    • 我现在已经编辑了我的答案。如果它做你想要的。请考虑接受。谢谢
    猜你喜欢
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 2019-08-02
    • 2014-03-12
    相关资源
    最近更新 更多