Totaling within loop to sum up for an array index

I currently have an array called contestData that looks like this:

"BUNDLE" => array: [
	"audio"=> array:[
		"downloaded"=> [
			"this_week"=> "365"
			"today" => 987
		]
		"saved"=> [
			"this_week"=> "56"
			"today" => 193
		]
	]
	"video"=> array:[
		"downloaded"=> [
			"this_week"=> "365"
			"today" => 987
		]
		"saved"=> [
			"this_week"=> "56"
			"today" => 193
		]
	]
]

That shows this structure:

['BUNDLE']['audio']['downloaded']['this_week']
['BUNDLE']['video']['downloaded']['today']
['BUNDLE']['audio']['saved']['this_week']
['BUNDLE']['video']['saved']['today']

But now I’m trying to figure out how to, within a loop on this array, add the totals into a totals index called contestTotals like this:

//total for bundles saved this week and today
['BUNDLE']['contestTotals']['saved']['this_week']
['BUNDLE']['contestTotals']['saved']['today']


//total for bundled downloaded this week and today
['BUNDLE']['contestTotals']['downloaded']['this_week']
['BUNDLE']['contestTotals']['downloaded']['today']

So at that point I’m not worried about the audio or video type but simply totaling for the bundle type

Here is the loop, but I’m unsure of how to properly add and save the totals in this way

foreach ($this->contestData as $type => &$typeData) {
    if ($type !== 'contestTotals') {
        foreach ($typeData as $bundleType => &$bundleTypeData) {
            if(!isset($bundleTypeData['saved'])) $bundleTypeData['saved'] = [];
            foreach ($this->interval as $intervals){
                if(!isset($bundleTypeData['saved'][$intervals]))
                    $bundleTypeData['saved'][$intervals] = 0;
            }
            foreach ($this->interval as $intervals){
                if(!isset($prdCatData['downloaded'][$intervals]))
                    $prdCatData['downloaded'][$intervals] = 0;
            }
        }
    }
}

I’m fond of using array functions to operate on sets of data -

$result = array_merge_recursive($contestData['BUNDLE']['audio'],$contestData['BUNDLE']['video']);
// examine data
echo '<pre>'; print_r($result); echo '</pre>';

// call-back function to sum supplied arrays
function _sum($arr)
{
	return array_sum($arr);
}

$result['downloaded'] = array_map('_sum',$result['downloaded']);
$result['saved'] = array_map('_sum',$result['saved']);
// examine data
echo '<pre>'; print_r($result);  echo '</pre>';
Sponsor our Newsletter | Privacy Policy | Terms of Service