Home  >  Article  >  Backend Development  >  How to Merge Associative Arrays with Missing Columns and Provide Default Values?

How to Merge Associative Arrays with Missing Columns and Provide Default Values?

Barbara Streisand
Barbara StreisandOriginal
2024-10-20 21:40:02642browse

How to Merge Associative Arrays with Missing Columns and Provide Default Values?

Merging Associative Arrays with Missing Columns and Default Values

Given an array of associative arrays, the task is to combine them, merging the array keys and filling the missing columns with a default value.

<code class="php">$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);</code>
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',
  ),
)

The desired output is:

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

To achieve this, array_merge() can be used.

<code class="php">$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 class="brush:php;toolbar:false">';
print_r($data);</code>

An alternative method is to create key pair values and then map each $d and merge.

<code class="php">$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);</code>

The above is the detailed content of How to Merge Associative Arrays with Missing Columns and Provide Default Values?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn