我有以下数据(我还有更多)。
$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}}}';
最初我创建了一个循环,所以我可以遍历那些(我在示例中只展示了2个)
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>";
//
}
}
我需要在循环中执行以下操作:
1)创建一个新数组并添加Country + Total Number
2)如果Country中已存在Country,则只需将Total Number添加到现有项目中
3)最后,我将有一个数组,其中只包含每个不同国家/地区的总视图。
我需要一些帮助,有人可以提供帮助:)
答案 0 :(得分:0)
这里我们使用简单的foreach
循环和function
来添加结果数组中的值。
<?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"];
}
}
}
}