Ortix Posted May 20, 2012 Share Posted May 20, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262818-calculations-on-multidimensional-array/ Share on other sites More sharing options...
.josh Posted May 20, 2012 Share Posted May 20, 2012 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; } } } Quote Link to comment https://forums.phpfreaks.com/topic/262818-calculations-on-multidimensional-array/#findComment-1347099 Share on other sites More sharing options...
Ortix Posted May 20, 2012 Author Share Posted May 20, 2012 Thanks for your reply .josh! Appreciate it, however the keys competitive set and test are arbitrary. They can can be anything so this isn't a full solution, just for this particular problem. Quote Link to comment https://forums.phpfreaks.com/topic/262818-calculations-on-multidimensional-array/#findComment-1347123 Share on other sites More sharing options...
Jessica Posted May 20, 2012 Share Posted May 20, 2012 So make it a function with that key as an argument. Quote Link to comment https://forums.phpfreaks.com/topic/262818-calculations-on-multidimensional-array/#findComment-1347130 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.