首先根据值合并两个数组,然后根据key-php合并第二个数组

时间:2014-04-07 13:05:43

标签: php arrays multidimensional-array

我有两个数组

第一个名为" $ periods"的数组和第二个数组名为" $ groupExport"如下。

First Array - $ periods

Array
(
    [0] => 201308
    [1] => 201309
    [2] => 201310
)

第二个数组 - $ groupExport

Array
(
    [0] => Array
        (
            [GroupCode] => 111
            [GroupDesc] => Crop Production
            [201308] => 1.5500
            [201309] => 240.4200
            [201310] => 41.2110
        )

    [1] => Array
        (
            [GroupCode] => 112
            [GroupDesc] => Animal Production
            [201309] => 3.1800
        )

    [2] => Array
        (
            [GroupCode] => 115
            [GroupDesc] => Agriculture, Forestry Support
            [201308] => 234.0400
            [201310] => 343.0200
        )
)

我尝试了至少5-6种不同的代码思想,它不会产生以下输出。我想要输出如下所示,以便我可以根据标题GroupCode,GroupDesc,201308,201309& amp; 201310.任何人都可以帮我解决这个问题吗?

输出

Array
(
    [0] => Array
        (
            [GroupCode] => 111
            [GroupDesc] => Crop Production
            [201308] => 1.5500
            [201309] => 240.4200
            [201310] => 41.2110
        )

    [1] => Array
        (
            [GroupCode] => 112
            [GroupDesc] => Animal Production
            [201308] => 0
            [201309] => 3.1800
            [201310] => 0
        )

    [2] => Array
        (
            [GroupCode] => 115
            [GroupDesc] => Agriculture, Forestry Support
            [201308] => 234.0400
            [201309] => 0
            [201310] => 343.0200
        )
)

2 个答案:

答案 0 :(得分:3)

循环遍历$groupExport数组并在每次迭代时循环遍历子数组并检查每个句点索引是否存在。如果他们不这样做,请用0初始化它们。

foreach ($groupExport as & $export) {
    foreach ($periods as $period) {
        if (!isset($export[$period])) {
            $export[$period] = 0;
        }
    }
}

注意&之前的$export。这意味着我们通过引用传递数组 - 它在每次迭代时修改$groupExport

Demo

答案 1 :(得分:2)

注意:这会改变原始数组。如果你想制作一个新的阵列,@ AmalMurali的解决方案是优越的。

这是使用PHP内置函数的替代解决方案:

$keys = array_fill_keys($periods, 0);
array_walk($groupExport, function (&$item, $key, $keys) {
    $item += $keys;
}, $keys);

这里是demonstration at IDEOne.com


解释

$keys = array_fill_keys($periods, 0);

这会将您的句点数组转换为一个新数组,其中原始数组的值现在是键,值为0

array_walk($groupExport, function (&$item, $key, $keys) {
    $item += $keys;
}, $keys);

我们遍历数组$groupExport的每个元素,并将函数应用于每个成员。我们将每个数组元素作为对用户定义函数的引用传递,这允许我们直接操作它。

最后,我们将$keys添加到$item,它基本上合并了两个数组,同时保持了关键关联。

输出

Array
(
    [0] => Array
        (
            [GroupCode] => 111
            [GroupDesc] => Crop Production
            [201308] => 1.55
            [201309] => 240.42
            [201310] => 41.211
        )

    [1] => Array
        (
            [GroupCode] => 112
            [GroupDesc] => Animal Production
            [201309] => 3.18
            [201308] => 0
            [201310] => 0
        )

    [2] => Array
        (
            [GroupCode] => 115
            [GroupDesc] => Agriculture, Forestry Support
            [201308] => 234.04
            [201310] => 343.02
            [201309] => 0
        )

)