合并多个关联数组并添加缺失的列及默认值

9

I have the following code:

$a = array('a' => 'some value', 'b' => 'some value', 'c' => 'some value');
$b = array('a' => 'another value', 'd' => 'another value', 'e' => 'another value', 'f' => 'another value');
$c = array('b' => 'some more value', 'x' => 'some more value', 'y' => 'some more value', 'z' => 'some more value');

$d = array($a, $b, $c);

var_export($d)将输出:

array (
  0 => 
  array (
    'a' => 'some value',
    'b' => 'some value',
    'c' => 'some value',
  ),
  1 => 
  array (
    'a' => 'another value',
    'd' => 'another value',
    'e' => 'another value',
    'f' => 'another value',
  ),
  2 => 
  array (
    'b' => 'some more value',
    'x' => 'some more value',
    'y' => 'some more value',
    'z' => 'some more value',
  ),
)

我该如何将数组键合并并得到以下输出?

Array
(
    [0] => Array
        (
            [a] => some value
            [b] => some value
            [c] => some value
            [d] => 
            [e] => 
            [f] => 
            [x] => 
            [y] => 
            [z] => 
        )

    [1] => Array
        (
            [a] => another value
            [b] => 
            [c] => 
            [d] => another value
            [e] => another value
            [f] => another value
            [x] => 
            [y] => 
            [z] => 
        )

    [2] => Array
        (
            [a] => 
            [b] => some more value
            [c] => 
            [d] => 
            [e] => 
            [f] => 
            [x] => some more value
            [y] => some more value
            [z] => some more value
        )
)
3个回答

9

您可以在这种情况下使用 array_merge

$a = array('a' => 'some value', 'b' => 'some value', 'c' => 'some value');
$b = array('a' => 'another value', 'd' => 'another value', 'e' => 'another value', 'f' => 'another value');
$c = array('b' => 'some more value', 'x' => 'some more value', 'y' => 'some more value', 'z' => 'some more value');
$d = array($a, $b, $c);
$keys = array();
foreach(new RecursiveIteratorIterator(new RecursiveArrayIterator($d)) as $key => $val) $keys[$key] = '';
$data = array();
foreach($d as $values) {
    $data[] = array_merge($keys, $values);
}

echo '<pre>';
print_r($data);

编辑:另一种方法是创建与空配对的键值对,然后映射每个$d并合并:

$keys = array_keys(call_user_func_array('array_merge', $d));
$key_pair = array_combine($keys, array_fill(0, count($keys), null));
$values = array_map(function($e) use ($key_pair) {
    return array_merge($key_pair, $e);
}, $d);

4
$a = array('a' => 'some value', 'b' => 'some value', 'c' => 'some value');
$b = array('a' => 'another value', 'd' => 'another value', 'e' => 'another value', 'f' => 'another value');
$c = array('b' => 'some more value', 'x' => 'some more value', 'y' => 'some more value', 'z' => 'some more value');

$d = array_merge(array_merge($a, $b),$c);

foreach($d as $k=>$v){
    $aN[$k] = isset($a[$k])?$a[$k]:''; 
    $bN[$k] = isset($b[$k])?$b[$k]:''; 
    $cN[$k] = isset($c[$k])?$c[$k]:''; 
}
$dN = array($aN, $bN, $cN );

不错。这正是我期望的输出。但对于实际情况,我有比那更多的数组和键。你能看到一种自动化处理n个数组和n个键的方法吗? - MrUpsidown
对于这个问题,@Ghost的答案很好! - Shaunak Shukla
是的,那正是我現在正在檢查的。無論如何,還是謝謝你的幫助! - MrUpsidown

0
使用所有数组中找到的唯一键创建默认元素的关联数组。
然后迭代每个数组,并用遇到的每一行覆盖默认数据。
代码:(演示)
$defaults = array_fill_keys(array_keys($a + $b + $c), '');
var_export(
    array_map(fn($row) => array_merge($defaults, $row), [$a, $b, $c])
);

或者,如果你只想使用$d:(演示)

$defaults = array_fill_keys(array_keys(array_merge(...$d)), '');
var_export(
    array_map(fn($row) => array_merge($defaults, $row), $d)
);

如果您需要保留第一级键,则可以使用经典的foreach()循环或array_walk()进行迭代,然后确保通过引用修改行。(演示

$defaults = array_fill_keys(array_keys($a + $b + $c), '');
$result = ['one' => $a, 'two' => $b, 'three' => $c];
foreach ($result as $k => &$row) {
    $row = array_merge($defaults, $row);
}
var_export($result);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接