1

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 :)

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
Vicky Dallas
  • 15
  • 1
  • 5

1 Answers1

0

Here we are using simple foreach loop and a function which adds the values in the result array.

Try this code snippet here

<?php

ini_set('display_errors', 1);
$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}}}';


$result=array();
getCount(json_decode($item1,true));
getCount(json_decode($item2,true));
print_r($result);
function getCount($array1)
{
    global $result;
    foreach($array1 as $key => $value)
    {
        foreach($value as $country => $data)
        {
            if(!isset($result[$country]))
            {
                $result[$country]=$data["total"];
            }
            else
            {
                $result[$country]+=$data["total"];
            }
        }
    }
}
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42