我有以下代码:
$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
)
)
是的,在这种情况下可以使用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);
$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 );
使用所有数组中的所有唯一键创建默认元素的关联数组。
然后迭代每个数组,并用遇到的每一行覆盖默认数据。
代码:(演示)
$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);