Skip to content
Advertisement

fill in the multidimensional array with missing keys

I have the following multidimensional array. I had to create keys the way it looks to group them accordingly.

Array
(
    [Oranges] => Array
        (
            [Name] => Oranges
            [l.VA123] => 17
            [l.MA123] => 12
            [l.GA123] => 9
            [l.CT123] => 5
        )

    [Apple] => Array
        (
            [Name] => Apple
            [l.CA123] => 13
        )

    [Grapes] => Array
        (
            [Name] => Grapes
            [l.WI123] => 8
            [l.FL123] => 5
        )
)

However, I need all the subarrays to have the same keys. Missing ones should be filled with a value of 0. The final array should be like below so that all subarrays have equal length.

    Array
(
    [Oranges] => Array
        (
            [Name] => Oranges
            [l.VA123] => 17
            [l.MA123] => 12
            [l.GA123] => 9
            [l.CT123] => 5
            [l.CA123] => 0
            [l.WI123] => 0
            [l.FL123] => 0
        )

    [Apple] => Array
        (
            [Name] => Apple
            [l.CA123] => 13
            [l.WI123] => 0
            [l.FL123] => 0
            [l.VA123] => 0
            [l.MA123] => 0
            [l.GA123] => 0
            [l.CT123] => 0
        )

    [Grapes] => Array
        (
            [Name] => Grapes
            [l.WI123] => 8
            [l.FL123] => 5
            [l.CA123] => 0
            [l.VA123] => 0
            [l.MA123] => 0
            [l.GA123] => 0
            [l.CT123] => 0
        )
)

Advertisement

Answer

You need a simple + operator. As from manual:

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

$items = Array
(
    'Oranges' => Array
        (
            'Name' => 'Oranges',
            'l.VA123' => 17,
            'l.MA123' => 12,
            'l.GA123' => 9,
            'l.CT123' => 5,
        ),

    'Apple' => Array
        (
            'Name' => 'Apple',
            'l.CA123' => 13,
        ),

    'Grapes' => Array
        (
            'Name' => 'Grapes',
            'l.WI123' => 8,
            'l.FL123' => 5,
        ),
);

// static keys
$keys = [
    'l.VA123' => 0,
    'l.MA123' => 0,
    'l.GA123' => 0,
    'l.CT123' => 0,
    'l.CA123' => 0,
    'l.WI123' => 0,
    'l.FL123' => 0,
];


// keys generated from source array, tricky approach
$keys = array_fill_keys(
    // here we merge all elements of `$items` into one array
    // as keys are repeated - you definitely got all keys that
    // can be in `$items`, `array_keys` will give you these keys
    // `array_fill_keys` will create array where key is what you need
    // and value is 0.
    array_keys(call_user_func_array('array_merge', $items)),
    0
);

// keys generated from source array, SIMPLE approach
$keys = [];
foreach ($items as $item) {
    foreach ($item as $k => $v) {
        if ($k != 'Name') {
            $keys[$k] = 0;
        }
    }
}

foreach ($items as &$item) {
    $item = $item + $keys;
}
print_r($items);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement