用缺少的键填充多维数组

时间:2017-03-21 19:39:08

标签: php arrays multidimensional-array

我有以下多维数组。我必须按照它们的相应方式创建键。

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

但是,我需要所有子阵列具有相同的键。缺少的值应填充值0.最后的数组应如下所示,以便所有子数组具有相同的长度。

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

2 个答案:

答案 0 :(得分:3)

您需要一个简单的+运算符。从manual开始:

  

+运算符返回附加到左侧数组的右侧数组;对于存在于两个数组中的键,将使用左侧数组中的元素,并且将忽略右侧数组中的匹配元素。

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

答案 1 :(得分:1)

可能有人可以提出更有效率的东西,但如果没有你想要的密钥列表,我认为你需要接受一些数组的传递:

<?php
$fruits = [
    "Oranges"=>["Name"=>"Oranges", "l.VA123"=>17, "l.MA123"=>12, "1.GA123"=>9, "1.CT123"=>5],
    "Apple"=>["Name"=>"Apple", "1.CA123"=>13],
    "Grapes"=>["Name"=>"Grapes", "1.WI123"=>8, "1.FL123"=>5]
];
$keys = [];
foreach ($fruits as $fruit) {
    unset($fruit["Name"]);
    $keys = array_merge($keys, array_keys($fruit));
}
$keys = array_fill_keys(array_unique($keys), 0);

foreach ($fruits as &$fruit) {
    $fruit = array_merge($keys, $fruit);
}
print_r($fruits);