Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP在数组之间添加缺少的键_Php_Arrays - Fatal编程技术网

PHP在数组之间添加缺少的键

PHP在数组之间添加缺少的键,php,arrays,Php,Arrays,我有以下代码: $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'

我有以下代码:

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

)

提前感谢您对如何实现这一目标的帮助。

是的,在这种情况下,您可以使用
array\u 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_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 );
$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);

这将是一个有趣的循环过山车。当我吸了一支烟,如果没有其他人吸过,我会回答的,然后初始化一个数组并合并它们@OleHaugset实际上不需要(手动)循环,但仍然不能给他上面发布的想要的结果。这只是将两个数组合并到一个数组中。在MultiArrayVM中没有空键,请第三次阅读问题:PNice。这给出了我所期望的结果。但对于真实场景,我有更多的数组和键。你能想到一种方法来自动化n个数组和n个键的处理吗?因为@Ghost的答案很好!是的,这正是我现在要检查的。无论如何谢谢你的帮助!完美的那正是我想要的。非常感谢你的帮助!
$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);