robjohnston Posted July 2, 2010 Share Posted July 2, 2010 Hi all, can anyone think of a more elegant way of doing this? TIA. // Accumulate the region total $aRegionTotal['sales']['units']+= $aNettSummary['sales']['units']; $aRegionTotal['sales']['HP']+= $aNettSummary['sales']['HP']; $aRegionTotal['sales']['fee']+= $aNettSummary['sales']['fee']; $aRegionTotal['cancels']['units']+= $aNettSummary['cancels']['units']; $aRegionTotal['cancels']['HP']+= $aNettSummary['cancels']['HP']; $aRegionTotal['cancels']['fee']+= $aNettSummary['cancels']['fee']; $aRegionTotal['nett']['units']+= $aNettSummary['nett']['units']; $aRegionTotal['nett']['HP']+= $aNettSummary['nett']['HP']; $aRegionTotal['nett']['fee']+= $aNettSummary['nett']['fee']; // Accumulate the company total $aCompanyTotal['sales']['units']+= $aNettSummary['sales']['units']; $aCompanyTotal['sales']['HP']+= $aNettSummary['sales']['HP']; $aCompanyTotal['sales']['fee']+= $aNettSummary['sales']['fee']; $aCompanyTotal['cancels']['units']+= $aNettSummary['cancels']['units']; $aCompanyTotal['cancels']['HP']+= $aNettSummary['cancels']['HP']; $aCompanyTotal['cancels']['fee']+= $aNettSummary['cancels']['fee']; $aCompanyTotal['nett']['units']+= $aNettSummary['nett']['units']; $aCompanyTotal['nett']['HP']+= $aNettSummary['nett']['HP']; $aCompanyTotal['nett']['fee']+= $aNettSummary['nett']['fee']; Quote Link to comment https://forums.phpfreaks.com/topic/206526-adding-array-values/ Share on other sites More sharing options...
Adam Posted July 2, 2010 Share Posted July 2, 2010 How are you defining the arrays? Quote Link to comment https://forums.phpfreaks.com/topic/206526-adding-array-values/#findComment-1080311 Share on other sites More sharing options...
kenrbnsn Posted July 2, 2010 Share Posted July 2, 2010 Use arrays of keys and a foreach loop <?php $keys1 = array('sales','cancels','nett'); $keys2 = array('units','HP','fee'); foreach ($keys1 as $k1) { foreach ($keys2 as $k2) { $aRegionTotal[$k1][$k2]+= $aNettSummary[$k1][$k2]; $aCompanyTotal[$k1][$k2]+= $aNettSummary[$k1][$k2]; } } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/206526-adding-array-values/#findComment-1080318 Share on other sites More sharing options...
robjohnston Posted July 6, 2010 Author Share Posted July 6, 2010 Thanks, that's the way I was heading. I wondered if there was a technique that I was missing. Cheers, Rob. Quote Link to comment https://forums.phpfreaks.com/topic/206526-adding-array-values/#findComment-1082026 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.