I have the following data (i have a lot more).
$item1 = '{"views":{"Greece":{"total":10},"Cyprus":{"total":23},"Bulgaria":{"total":1},"United States":{"total":53},"United Kingdom":{"total":11},"Spain":{"total":2},"India":{"total":1},"":{"total":1},"Finland":{"total":2},"France":{"total":2},"Sweden":{"total":1},"Germany":{"total":5},"Ireland":{"total":1},"Austria":{"total":2},"Switzerland":{"total":2},"Poland":{"total":1},"Mexico":{"total":1}},"clicks":{"Greece":{"total":310},"Bulgaria":{"total":1},"United Kingdom":{"total":4},"United States":{"total":3},"Cyprus":{"total":15},"Finland":{"total":1},"Germany":{"total":1},"Ireland":{"total":1},"Spain":{"total":1},"Poland":{"total":1}},"leads":{"Greece":{"total":9}}}';
$item2 = '{"views":{"Greece":{"total":15},"Cyprus":{"total":1},"Bulgaria":{"total":1},"United States":{"total":53},"United Kingdom":{"total":11},"Spain":{"total":2},"India":{"total":1},"":{"total":1},"Finland":{"total":2},"France":{"total":2},"Sweden":{"total":1},"Germany":{"total":5},"Ireland":{"total":1},"Austria":{"total":2},"Switzerland":{"total":2},"Poland":{"total":1},"Mexico":{"total":1}},"clicks":{"Greece":{"total":310},"Bulgaria":{"total":1},"United Kingdom":{"total":4},"United States":{"total":3},"Cyprus":{"total":15},"Finland":{"total":1},"Germany":{"total":1},"Ireland":{"total":1},"Spain":{"total":1},"Poland":{"total":1}},"leads":{"Greece":{"total":9}}}';
Initially I created a loop so i can loop through those (i am presenting only 2 in my example)
for ( $x = 1; $x <= 2; $x++ ) {
$theitem = 'item' . $x;
$countries = json_decode( $$theitem, true );
$keys = array_keys( $countries[ 'views' ] );
$arraySize = count( $countries[ 'views' ] );
for ( $i = 0; $i < $arraySize; $i++ ) {
if ( $keys[ $i ] == '' ) {
echo "Empty: ";
} else {
echo $keys[ $i ] . ": ";
}
echo $countries[ 'views' ][ $keys[ $i ] ][ 'total' ];
echo "<br>";
//
}
}
What I need to do in the loop is the following:
1) Create a New array and add Country + Total Number
2) If Country already exists in the Array just add the Total Number to the already existing item)
3) Finally i will have 1 array with just the total views of each distinct Country.
I need some help in doing that, can anyone help :)