dshmvqnl98119 2012-05-21 17:52
浏览 18

多维数组的计算

I'm working on a project and I can't wrap my head around on a calculation that needs to be done on a multidimensional array. Quick background, it's a hotel benchmarking tool and I need to calculate the market penetration index (MPI). I have an array with 3 main arrays. First 2 are the hotels which are being compared and the last one is the MPI array.

Each array contains an array for every month the user selects. Inside THAT array is data that needs to be used for calculations. Here is an example:

Array
(
 [Competitive set] => Array
    (
        [Sep 11] => Array
            (
                [0] => Array
                    (
                        [minmonth] => 2011-09-01
                        [maxmonth] => 2011-09-01
                        [nrcheck] => 13
                        [data] => 67.6
                    )

            )

        [Oct 11] => Array
            (
                [0] => Array
                    (
                        [minmonth] => 2011-10-01
                        [maxmonth] => 2011-10-01
                        [nrcheck] => 13
                        [data] => 63.6
                    )

            )

        [Nov 11] => Array
            (
                [0] => Array
                    (
                        [minmonth] => 2011-11-01
                        [maxmonth] => 2011-11-01
                        [nrcheck] => 13
                        [data] => 59.2
                    )

            )

        [Dec 11] => Array
            (
                [0] => Array
                    (
                        [minmonth] => 2011-12-01
                        [maxmonth] => 2011-12-01
                        [nrcheck] => 13
                        [data] => 54.6
                    )

            )

    )

[Test] => Array
    (
        [Sep 11] => Array
            (
                [0] => Array
                    (
                        [minmonth] => 2011-09-01
                        [maxmonth] => 2011-09-01
                        [nrcheck] => 89
                        [data] => 71.5
                    )

            )

        [Oct 11] => Array
            (
                [0] => Array
                    (
                        [minmonth] => 2011-10-01
                        [maxmonth] => 2011-10-01
                        [nrcheck] => 89
                        [data] => 67.0
                    )

            )

        [Nov 11] => Array
            (
                [0] => Array
                    (
                        [minmonth] => 2011-11-01
                        [maxmonth] => 2011-11-01
                        [nrcheck] => 91
                        [data] => 63.1
                    )

            )

        [Dec 11] => Array
            (
                [0] => Array
                    (
                        [minmonth] => 2011-12-01
                        [maxmonth] => 2011-12-01
                        [nrcheck] => 89
                        [data] => 57.5
                    )

            )

    )

[MPI] => Array
    (
        [Sep 11] => Array
            (
                [0] => Array
                    (
                        [minmonth] => 2011-09-01
                        [maxmonth] => 2011-09-01
                        [nrcheck] => 89
                        [data] => 71.5
                    )

            )

        [Oct 11] => Array
            (
                [0] => Array
                    (
                        [minmonth] => 2011-10-01
                        [maxmonth] => 2011-10-01
                        [nrcheck] => 89
                        [data] => 67.0
                    )

            )

        [Nov 11] => Array
            (
                [0] => Array
                    (
                        [minmonth] => 2011-11-01
                        [maxmonth] => 2011-11-01
                        [nrcheck] => 91
                        [data] => 63.1
                    )

            )

        [Dec 11] => Array
            (
                [0] => Array
                    (
                        [minmonth] => 2011-12-01
                        [maxmonth] => 2011-12-01
                        [nrcheck] => 89
                        [data] => 57.5
                    )

            )

    )

)

here is a pastebin with var_export as suggested (can't seem to format it propperly here) http://pastebin.com/b8hYM7TW Sorry for the huge code block but it had to be this big =( Either way currently the 'data' for MPI is wrong. That needs to become the data of the first array divided by the data of the second array and multiplied by 100 (percentage).

The keys "Competitive Set" and "Test" are variable and not available in the scope i'm working in so those static names can't be used.

I read about recursive functions but I'm still not getting it...

  • 写回答

1条回答 默认 最新

  • duandi5328 2012-05-30 20:46
    关注

    Well the pastebin array you provided doesn't have any data in the "Dit hotel" array, so I can't really confirm this works, but here you go

    foreach ($array['Dit hotel'] as $key => $value) {
    
        $array['MPI'][$key][0]['data'] = $value[0]['data'] / $array['Test'][$key][0]['data'] * 100;
    }
    

    http://codepad.org/m884GMpJ

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度