doulu3865 2017-04-29 12:06
浏览 206
已采纳

PHP:将多个jsons的数据组合到数组中

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

  • 写回答

1条回答 默认 最新

  • dptdb84606 2017-04-29 12:13
    关注

    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"];
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看