0

我想在 PHP 中组合两个多维数组。

打印_r($array_a):

Array
(
[0] => Array
    (
        [0] => A
        [1] => 0
        [2] => 1047
    )

[1] => Array
    (
        [0] => B
        [1] => 0
        [2] => 279
    )

[2] => Array
    (
        [0] => C
        [1] => 0
        [2] => 68
    )

[3] => Array
    (
        [0] => D
        [1] => 0
        [2] => 4
    )

)

打印_r($array_b):

Array
(
[0] => Array
    (
        [0] => A
        [1] => 76
        [2] => 0
    )

[1] => Array
    (
        [0] => B
        [1] => 170
        [2] => 0
    )

[2] => Array
    (
        [0] => C
        [1] => 15
        [2] => 0
    )

[3] => Array
    (
        [0] => D
        [1] => 210
        [2] => 0
    )
[4] => Array
    (
        [0] => E
        [1] => 287
        [2] => 0
    )

)

然后合并它,结果应该是这样的:

Array
(
[0] => Array
    (
        [0] => A
        [1] => 76
        [2] => 1047
    )

[1] => Array
    (
        [0] => B
        [1] => 170
        [2] => 279
    )

[2] => Array
    (
        [0] => C
        [1] => 15
        [2] => 68
    )

[3] => Array
    (
        [0] => D
        [1] => 210
        [2] => 4
    )
[4] => Array
    (
        [0] => E
        [1] => 287
        [2] => 0
    )

)

所以逻辑关键是根据每个数组的第一个值(A,B,C,D)合并两个数组。如果有一个数组只属于其中一个数组(例如,array_b 上的“E”),我只需添加它们。我试过这个:

foreach($array_a as $a=>$array_now){
        foreach($array_b as $b=>$array_before){
            if($array_now[0] == $array_before[0]){
                $array_a[$a] = [$array_before[0], $array_before[1], $array_now[2]];
            }
        }
    }

但我似乎无法添加缺失的数组($array_b 中的第 5 个数组,其中包含“E”)。

4

4 回答 4

1

使用下面的代码可以帮助你

$array_a = [];
$array_a[0][0] = 'A';
$array_a[0][1] = 0;
$array_a[0][2] = 1047;

$array_a[1][0] = 'B';
$array_a[1][1] = 0;
$array_a[1][2] = 279;

$array_a[2][0] = 'C';
$array_a[2][1] = 0;
$array_a[2][2] = 68;

$array_a[3][0] = 'D';
$array_a[3][1] = 0;
$array_a[3][2] = 4;


$array_b = [];
$array_b[0][0] = 'A';
$array_b[0][1] = 76;
$array_b[0][2] = 0;

$array_b[1][0] = 'B';
$array_b[1][1] = 70;
$array_b[1][2] = 0;

$array_b[2][0] = 'C';
$array_b[2][1] = 15;
$array_b[2][2] = 0;

$array_b[3][0] = 'D';
$array_b[3][1] = 210;
$array_b[3][2] = 0;

$array_b[3][0] = 'E';
$array_b[3][1] = 287;
$array_b[3][2] = 0;

$array_b[4][0] = 'E';
$array_b[4][1] = 2870;
$array_b[4][2] = 0;


$cnt = 0;
if(count($array_b) > $array_a){
    $cnt =  count($array_b);
}else{
    $cnt =  count($array_a);
}


$array_new = [];
for($i=0; $i <= $cnt; $i++){
    $array_new[$i] = [];

    if(isset($array_a[$i][0])){
        $array_new[$i][0] = $array_a[$i][0];
    }else{
        $array_new[$i][0] = $array_b[$i][0];    
    }   

    if(isset($array_b[$i][1])){
        $array_new[$i][1] = $array_b[$i][1];
    }else{
        $array_new[$i][1] = 0;  
    }

    if(isset($array_a[$i][2])){
        $array_new[$i][2] = $array_a[$i][2];
    }else{
        $array_new[$i][2] = 0;  
    }


}

echo "<pre>";
print_r($array_new);

希望这会有所帮助!

于 2016-11-25T11:16:46.580 回答
0

抱歉,上述所有解决方案都不起作用,我自己想通了,通过使用以下代码:

    $result = array();
    if (count($array_a) > count($array_b)){
        foreach($array_a as $a=>$array_now){
            foreach($array_b as $b=>$array_before){
                if($array_now[0] == $array_before[0]){
                    $array_a[$a] = [(string)$array_before[0], (int)$array_before[1], (int)$array_now[2]];
                }
            }
        }
        $result = $array_a;
    } else {
        foreach($array_b as $a=>$array_now){
            foreach($array_a as $b=>$array_before){
                if($array_now[0] == $array_before[0]){
                    $array_b[$a] = [(string)$array_before[0], (int)$array_now[1], (int)$array_before[2]];
                }
            }
        }
        $result = $array_b;
    }

谢谢大家的回复。

于 2016-11-25T12:21:59.217 回答
0

这个任务可以通过一个循环简洁、高效、可靠地执行,该循环生成一个带有临时键的结果数组,以确定是否遇到新行。

代码:(演示

$array_a = [
  ['A', 0, 1047],
  ['B', 0, 279],
  ['C', 0, 68],
  ['D', 0, 4],
];

$array_b = [
  ['A', 76, 0],
  ['B', 170, 0],
  ['E', 287, 0],
  ['C', 15, 0],
  ['D', 210, 0],
];


foreach (array_merge($array_a, $array_b) as $row) {
    if (!isset($result[$row[0]])) {
        $result[$row[0]] = $row;
    } else {
        $result[$row[0]][1] += $row[1];
        $result[$row[0]][2] += $row[2];
    }
}
var_export(array_values($result));

输出:

array (
  0 => 
  array (
    0 => 'A',
    1 => 76,
    2 => 1047,
  ),
  1 => 
  array (
    0 => 'B',
    1 => 170,
    2 => 279,
  ),
  2 => 
  array (
    0 => 'C',
    1 => 15,
    2 => 68,
  ),
  3 => 
  array (
    0 => 'D',
    1 => 210,
    2 => 4,
  ),
  4 => 
  array (
    0 => 'E',
    1 => 287,
    2 => 0,
  ),
)
于 2021-01-17T12:33:59.947 回答
0

您可以使用多个array_maps来实现这一点

$c = array_map(function() {
    return array_map(function() {
        foreach (func_get_args() as $arg) {
            if ($arg) return $arg;
        }
        return func_get_arg(0);
    }, ...func_get_args());
}, $a, $b);

此代码还可以使用超过$a, $b- 只需添加尽可能多的数组作为参数

于 2016-11-25T11:09:49.947 回答