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) will output:

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',
  ),
)

How would I go about combining the array keys and end up with the following output?

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
        )
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
MrUpsidown
  • 21,592
  • 15
  • 77
  • 131

3 Answers3

9

Yes you can use array_merge in this case:

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

EDIT: Another way would be to create key pair values with keys paired with empty, then, map each $d and merge:

$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);
Kevin
  • 41,694
  • 12
  • 53
  • 70
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 );
Shaunak Shukla
  • 2,347
  • 2
  • 18
  • 29
0

Create an associative array of default elements using all unique keys found in all arrays.

Then iterate each array and overwrite the default data with each encountered row.

Code: (Demo)

$defaults = array_fill_keys(array_keys($a + $b + $c), '');
var_export(
    array_map(fn($row) => array_merge($defaults, $row), [$a, $b, $c])
);

Or if you only want to work with $d: (Demo)

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

If you need to preserve first level keys, then use a classic foreach() loop or array_walk() to iterate and then be sure to modify rows by reference. (Demo)

$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);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136