简体   繁体   English

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

[英]Merge two arrays first one based on value and second one based on key - php

I have two arrays 我有两个数组

First array named "$periods" and second array named "$groupExport" like below. 第一个数组名为“$ periods”,第二个数组名为“$ groupExport”,如下所示。

First Array - $periods First Array - $ periods

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

Second Array - $groupExport 第二个数组 - $ 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
        )
)

I have tried atleast 5-6 different code ideas, it does not yields the below output. 我已经尝试了至少5-6个不同的代码想法,它不会产生以下输出。 I want the output like below, so that I can display the resultset in table format according to the header GroupCode, GroupDesc, 201308, 201309 & 201310. Could anyone help me to resolve this? 我想要输出如下所示,以便我可以根据标题GroupCode,GroupDesc,201308,201309和201310以表格格式显示结果集。任何人都可以帮我解决这个问题吗?

Output 产量

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

Loop through the $groupExport array and on each iteration, loop through the sub-array and check if the each one of the period index exists. 循环遍历$groupExport数组并在每次迭代时循环遍历子数组并检查每个句点索引是否存在。 If they don't, initialize them with 0. 如果他们不这样做,用0初始化它们。

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

Note the & before $export . 注意$export before之前的& It means we're passing the array by reference — it's modifying $groupExport on each iteration. 这意味着我们通过引用传递数组 - 它在每次迭代时修改$groupExport

Demo 演示

Note: This will mutate the original array. 注意:这将改变原始数组。 If you want to produce a new array, @AmalMurali's solution is superior. 如果你想生产一个新阵列,@ AmalMurali的解决方案是优越的。

Here's an alternate solution that uses some of PHP's built-in functions: 这是一个使用PHP的一些内置函数的替代解决方案:

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

Here's a demonstration at IDEOne.com . 这是IDEOne.com演示


Explanation 说明

$keys = array_fill_keys($periods, 0);

This converts your array of periods into a new array where the value from the original array is now the key, and the value is 0 . 这会将您的句点数组转换为一个新数组,其中原始数组的值现在是键,值为0

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

We walk over each element of the array $groupExport , and apply a function to each member. 我们遍历数组$groupExport每个元素,并将函数应用于每个成员。 We pass each array element as a reference to our user defined function, which allows us to manipulate it directly. 我们将每个数组元素作为对用户定义函数的引用传递,这允许我们直接操作它。

Finally, we add $keys to $item which essentially merges the two arrays while maintaining key associations. 最后,我们将$keys添加到$item ,它基本上合并了两个数组,同时保持了关键关联。

Output 产量

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
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM