如何分别计算某些子目录中 json 个对象的值总和
How to count sum of values from json objects in certain subdirectories seperately
我在主目录 2021
中有 3 个名为 september
、october
和 november
的目录。
在所有这 3 个目录中都是 json 个文件,如下所示:
{
"id": "id_2021-09-05_2200",
"date": "2021-09-05",
"guests": 32 // total guests for that day
}
对于每个月,我需要该月所有 guests
的总和。 (将当月所有 json 个文件中的所有访客数在一起)。
到目前为止我有这个代码,但我每个月都在计算:
$monthdirs = array_filter(glob('data/2021/*'), 'is_dir'); // read all month-dirs in year 2021
foreach($monthdirs as $monthdir) {
$monthfiles = glob($monthdir.'/*.json'); // all json files in a specific month
}
foreach($monthfiles as $monthfile) {
$arr[] = json_decode(file_get_contents($monthfile),true); // php assoc array
foreach($arr as $key => $val) {
$tot_guests_monthes[] += $val['guests']; // new array from each month with the sum of guests from all the json files in that month
}
}
foreach($tot_guests_monthes as $tot_guests_month) {
echo $tot_guests_month.'<br />';
}
我认为在第二个 foreach 循环中我做错了什么。我的最终输出应该是 3 个值,例如:
200 // all guests september
300 // all guests october
400 // all guests november
$...[] =
基本上是一个 array_push,这意味着 $tot_guests_monthes[] += $val['guests'];
将为每个 $monthfile
的 $arr
的每个元素创建一个新元素,而不是求和。
将一个月内的所有值相加,然后将它们添加到 $tot_guests_monthes
应该可行:
$tot_guests_monthes = [];
$monthdirs = array_filter(glob('data/2021/*'), 'is_dir'); // read all month-dirs in year 2021
foreach($monthdirs as $monthdir) {
$monthfiles = glob($monthdir.'/*.json'); // all json files in a specific month
$sum = 0;
foreach($monthfiles as $monthfile) {
$arr = json_decode(file_get_contents($monthfile), true);
$sum += $arr['guests'];
}
$tot_guests_monthes[] = $sum;
}
foreach($tot_guests_monthes as $tot_guests_month) {
echo $tot_guests_month.'<br />';
}
我在主目录 2021
中有 3 个名为 september
、october
和 november
的目录。
在所有这 3 个目录中都是 json 个文件,如下所示:
{
"id": "id_2021-09-05_2200",
"date": "2021-09-05",
"guests": 32 // total guests for that day
}
对于每个月,我需要该月所有 guests
的总和。 (将当月所有 json 个文件中的所有访客数在一起)。
到目前为止我有这个代码,但我每个月都在计算:
$monthdirs = array_filter(glob('data/2021/*'), 'is_dir'); // read all month-dirs in year 2021
foreach($monthdirs as $monthdir) {
$monthfiles = glob($monthdir.'/*.json'); // all json files in a specific month
}
foreach($monthfiles as $monthfile) {
$arr[] = json_decode(file_get_contents($monthfile),true); // php assoc array
foreach($arr as $key => $val) {
$tot_guests_monthes[] += $val['guests']; // new array from each month with the sum of guests from all the json files in that month
}
}
foreach($tot_guests_monthes as $tot_guests_month) {
echo $tot_guests_month.'<br />';
}
我认为在第二个 foreach 循环中我做错了什么。我的最终输出应该是 3 个值,例如:
200 // all guests september
300 // all guests october
400 // all guests november
$...[] =
基本上是一个 array_push,这意味着 $tot_guests_monthes[] += $val['guests'];
将为每个 $monthfile
的 $arr
的每个元素创建一个新元素,而不是求和。
将一个月内的所有值相加,然后将它们添加到 $tot_guests_monthes
应该可行:
$tot_guests_monthes = [];
$monthdirs = array_filter(glob('data/2021/*'), 'is_dir'); // read all month-dirs in year 2021
foreach($monthdirs as $monthdir) {
$monthfiles = glob($monthdir.'/*.json'); // all json files in a specific month
$sum = 0;
foreach($monthfiles as $monthfile) {
$arr = json_decode(file_get_contents($monthfile), true);
$sum += $arr['guests'];
}
$tot_guests_monthes[] = $sum;
}
foreach($tot_guests_monthes as $tot_guests_month) {
echo $tot_guests_month.'<br />';
}