doufei4923 2014-04-07 13:05
浏览 64
已采纳

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

I have two arrays

First array named "$periods" and second array named "$groupExport" like below.

First Array - $periods

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

Second Array - $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. 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?

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
        )
)
  • 写回答

2条回答 默认 最新

  • dongmaqiu6084 2014-04-07 13:10
    关注

    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. If they don't, initialize them with 0.

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

    Note the & before $export. It means we're passing the array by reference — it's modifying $groupExport on each iteration.

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab