Sum multi dimensional array based on a key in php
Posted
by
user1328178
on Stack Overflow
See other posts from Stack Overflow
or by user1328178
Published on 2012-04-12T05:13:06Z
Indexed on
2012/04/12
5:29 UTC
Read the original article
Hit count: 225
I have mutiple arrays say for now 2, first array has Cities(Mumbai, Pune. Bangalore, Hyderabad) And 2nd array has Mumbia nad Hyderabd.
I want to sum up the values based on the Key.
Array ( [Mumbai] => Array ( [ga:pageviews] => 1 [ga:visits] => 1 [ga:newVisits] => 1 [ga:pageviewsPerVisit] => 1 [ga:entranceBounceRate] => 1 [ga:avgTimeOnSite] => 1 [ga:timeOnSite] => 1 [ga:bounces] => 1 )
[Pune] => Array
(
[ga:pageviews] => 2
[ga:visits] => 2
[ga:newVisits] => 2
[ga:pageviewsPerVisit] => 2
[ga:entranceBounceRate] => 2
[ga:avgTimeOnSite] => 2
[ga:timeOnSite] => 2
[ga:bounces] => 2
)
[Bangalore] => Array
(
[ga:pageviews] => 3
[ga:visits] => 3
[ga:newVisits] => 3
[ga:pageviewsPerVisit] => 3
[ga:entranceBounceRate] => 3
[ga:avgTimeOnSite] => 3
[ga:timeOnSite] => 3
[ga:bounces] => 3
)
[Hyderabad] => Array
(
[ga:pageviews] => 4
[ga:visits] => 4
[ga:newVisits] => 4
[ga:pageviewsPerVisit] => 4
[ga:entranceBounceRate] => 4
[ga:avgTimeOnSite] => 4
[ga:timeOnSite] => 4
[ga:bounces] => 4
)
)
2nd Array:
Array ( [Mumbai] => Array ( [ga:pageviews] => 5 [ga:visits] => 5 [ga:newVisits] => 5 [ga:pageviewsPerVisit] => 5 [ga:entranceBounceRate] => 5 [ga:avgTimeOnSite] => 5 [ga:timeOnSite] => 5 [ga:bounces] => 5 )
[Hyderabad] => Array
(
[ga:pageviews] => 2
[ga:visits] => 2
[ga:newVisits] => 2
[ga:pageviewsPerVisit] => 2
[ga:entranceBounceRate] => 2
[ga:avgTimeOnSite] => 2
[ga:timeOnSite] => 2
[ga:bounces] => 2
)
)
I want the result as:
Array ( [Mumbai] => Array ( [ga:pageviews] => 6 [ga:visits] => 6 [ga:newVisits] => 6 [ga:pageviewsPerVisit] => 6 [ga:entranceBounceRate] => 6 [ga:avgTimeOnSite] => 6 [ga:timeOnSite] => 6 [ga:bounces] => 6 )
[Pune] => Array
(
[ga:pageviews] => 2
[ga:visits] => 2
[ga:newVisits] => 2
[ga:pageviewsPerVisit] => 2
[ga:entranceBounceRate] => 2
[ga:avgTimeOnSite] => 2
[ga:timeOnSite] => 2
[ga:bounces] => 2
)
[Bangalore] => Array
(
[ga:pageviews] => 3
[ga:visits] => 3
[ga:newVisits] => 3
[ga:pageviewsPerVisit] => 3
[ga:entranceBounceRate] => 3
[ga:avgTimeOnSite] => 3
[ga:timeOnSite] => 3
[ga:bounces] => 3
)
[Hyderabad] => Array
(
[ga:pageviews] => 6
[ga:visits] => 6
[ga:newVisits] => 6
[ga:pageviewsPerVisit] => 6
[ga:entranceBounceRate] => 6
[ga:avgTimeOnSite] => 6
[ga:timeOnSite] => 6
[ga:bounces] => 6
)
)
Thanks for your help.... Ruth.
© Stack Overflow or respective owner