Jump to content

Calculations on multidimensional array


Ortix

Recommended Posts

Hi guys, 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
                        )

                )

        )

)

 

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

 

How would I go about doing that? The foreach loops are a bit too big for me on this one.

Link to comment
Share on other sites


foreach ($array["MPI"] as $key => &$value) {
  foreach ($value as $k => &$v) {
    if ( isset($array["Competitive set"][$key][$k]['data'])&&isset($array["Test"][$key][$k]['data']) ) {
      $v['data'] = ( $array["Competitive set"][$key][$k]['data'] / $array["Test"][$key][$k]['data'] ) * 100;
    }
  }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.