Php 合并2个Json结果并计算特定值
我有许多JSON,例如: 第1项:Php 合并2个Json结果并计算特定值,php,arrays,json,Php,Arrays,Json,我有许多JSON,例如: 第1项: { "countries_views": [ { "thecount": "563", "country": "Greece" }, { "thecount": "48", "country": "United States" }, { "thecount
{
"countries_views": [
{
"thecount": "563",
"country": "Greece"
},
{
"thecount": "48",
"country": "United States"
},
{
"thecount": "11",
"country": "Luxembourg"
},
{
"thecount": "7",
"country": "Germany"
},
{
"thecount": "6",
"country": "Cyprus"
},
{
"thecount": "2",
"country": "India"
},
{
"thecount": "2",
"country": "France"
},
{
"thecount": "2",
"country": "United Kingdom"
},
{
"thecount": "1",
"country": "Nigeria"
},
{
"thecount": "1",
"country": "Russia"
}
]
}
{
"countries_views": [
{
"thecount": "1037",
"country": "Greece"
},
{
"thecount": "17",
"country": "United States"
},
{
"thecount": "17",
"country": "Cyprus"
},
{
"thecount": "12",
"country": "Germany"
},
{
"thecount": "11",
"country": ""
},
{
"thecount": "4",
"country": "United Kingdom"
},
{
"thecount": "4",
"country": "Australia"
},
{
"thecount": "2",
"country": "Belgium"
},
{
"thecount": "1",
"country": "Russia"
},
{
"thecount": "1",
"country": "Argentina"
}
]
}
第2项:
{
"countries_views": [
{
"thecount": "563",
"country": "Greece"
},
{
"thecount": "48",
"country": "United States"
},
{
"thecount": "11",
"country": "Luxembourg"
},
{
"thecount": "7",
"country": "Germany"
},
{
"thecount": "6",
"country": "Cyprus"
},
{
"thecount": "2",
"country": "India"
},
{
"thecount": "2",
"country": "France"
},
{
"thecount": "2",
"country": "United Kingdom"
},
{
"thecount": "1",
"country": "Nigeria"
},
{
"thecount": "1",
"country": "Russia"
}
]
}
{
"countries_views": [
{
"thecount": "1037",
"country": "Greece"
},
{
"thecount": "17",
"country": "United States"
},
{
"thecount": "17",
"country": "Cyprus"
},
{
"thecount": "12",
"country": "Germany"
},
{
"thecount": "11",
"country": ""
},
{
"thecount": "4",
"country": "United Kingdom"
},
{
"thecount": "4",
"country": "Australia"
},
{
"thecount": "2",
"country": "Belgium"
},
{
"thecount": "1",
"country": "Russia"
},
{
"thecount": "1",
"country": "Argentina"
}
]
}
等等!我需要做的是用PHP将这些数据合并到一个数组中,我需要添加重复的值。最终结果应该如下所示:
{
"countries_views": [
{
"thecount": "**1600**",
"country": "Greece"
},
{
"thecount": "**65**",
"country": "United States"
},
etcetcetc
]
}
首先解析JSON数据,我们只需遍历数组来找到重复项并将其相加
<?php
// emit the code of parsing json. (see function json_decode)
$arr1 = [
(object)['thecount' => 563, 'country' => 'Greece'],
(object)['thecount' => 12, 'country' => 'US'],
(object)['thecount' => 15, 'country' => 'UK'],
];
$arr2 = [
(object)['thecount' => 1563, 'country' => 'Greece'],
(object)['thecount' => 152, 'country' => 'CN'],
];
foreach ($arr1 as $each) {
$exists = false;
foreach ($arr2 as &$e) {
if ($e->country == $each->country) {
$e->thecount += $each->thecount;
$exists = true;
}
}
if (!$exists) {
array_push($arr2, $each);
}
}
$res = $arr2;
var_dump($res);
显示你的代码:)然后我可以试着帮你你好,阿德里安!我不知道该怎么做,我尝试过:json_解码和json_编码进行合并,但都没有达到我所需要的程度。首先,使用json_解码($json_object,true)
获取有效的php数组。然后,您必须创建一个函数,以您想要的方式合并解码的数组。