计算一个值在一个数组中发生的次数,该数组按另一个值(年)分组

时间:2019-05-06 09:18:49

标签: php arrays count

我有一个数组,里面有一些信息。

有两个重要的键datumanswer

这是我的数组:

Array
(
    [0] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 932
            [cid] => 202
            [answer] => ok
        )

    [1] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 930
            [cid] => 202
            [answer] => ok
        )

    [2] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 928
            [cid] => 202
            [answer] => ok
        )

    [3] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 926
            [cid] => 202
            [answer] => ok
        )

    [4] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 924
            [cid] => 202
            [answer] => ok
        )

    [5] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 922
            [cid] => 202
            [answer] => ok
        )

    [6] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 920
            [cid] => 202
            [answer] => ok
        )

    [7] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 918
            [cid] => 202
            [answer] => fout
        )

    [8] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 916
            [cid] => 202
            [answer] => ok
        )

    [9] => Array
        (
            [infoid] => 236
            [company_id] => 57
            [datum] => 2020
            [tid] => 324
            [catid] => 230
            [info_id] => 236
            [questionid] => 1192
            [cid] => 230
            [answer] => ok
        )

    [10] => Array
        (
            [infoid] => 236
            [company_id] => 57
            [datum] => 2020
            [tid] => 324
            [catid] => 230
            [info_id] => 236
            [questionid] => 1191
            [cid] => 230
            [answer] => ok
        )

    [11] => Array
        (
            [infoid] => 236
            [company_id] => 57
            [datum] => 2020
            [tid] => 324
            [catid] => 230
            [info_id] => 236
            [questionid] => 1190
            [cid] => 230
            [answer] => fout
        )

    [12] => Array
        (
            [infoid] => 236
            [company_id] => 57
            [datum] => 2020
            [tid] => 324
            [catid] => 230
            [info_id] => 236
            [questionid] => 1189
            [cid] => 230
            [answer] => ok
        )

    [13] => Array
        (
            [infoid] => 235
            [company_id] => 57
            [datum] => 2019
            [tid] => 329
            [catid] => 229
            [info_id] => 235
            [questionid] => 1187
            [cid] => 229
            [answer] => ok
        )

    [14] => Array
        (
            [infoid] => 235
            [company_id] => 57
            [datum] => 2019
            [tid] => 329
            [catid] => 228
            [info_id] => 235
            [questionid] => 1186
            [cid] => 228
            [answer] => ok
        )

    [15] => Array
        (
            [infoid] => 235
            [company_id] => 57
            [datum] => 2019
            [tid] => 329
            [catid] => 228
            [info_id] => 235
            [questionid] => 1185
            [cid] => 228
            [answer] => ok
        )

    [16] => Array
        (
            [infoid] => 236
            [company_id] => 57
            [datum] => 2020
            [tid] => 324
            [catid] => 230
            [info_id] => 236
            [questionid] => 1188
            [cid] => 230
            [answer] => ok
        )

    [17] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 934
            [cid] => 202
            [answer] => ok
        )

    [18] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 936
            [cid] => 202
            [answer] => ok
        )

    [19] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 938
            [cid] => 202
            [answer] => ok
        )

    [20] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 940
            [cid] => 202
            [answer] => ok
        )

    [21] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 942
            [cid] => 202
            [answer] => ok
        )

    [22] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 944
            [cid] => 202
            [answer] => ok
        )

    [23] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 946
            [cid] => 202
            [answer] => ok
        )

    [24] => Array
        (
            [infoid] => 215
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 202
            [info_id] => 215
            [questionid] => 948
            [cid] => 202
            [answer] => ok
        )

    [25] => Array
        (
            [infoid] => 235
            [company_id] => 57
            [datum] => 2019
            [tid] => 329
            [catid] => 228
            [info_id] => 235
            [questionid] => 1184
            [cid] => 228
            [answer] => ok
        )

    [26] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1154
            [cid] => 220
            [answer] => ok
        )

    [27] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1153
            [cid] => 220
            [answer] => ok
        )

    [28] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1152
            [cid] => 220
            [answer] => ok
        )

    [29] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1151
            [cid] => 220
            [answer] => ok
        )

    [30] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1150
            [cid] => 220
            [answer] => ok
        )

    [31] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1149
            [cid] => 220
            [answer] => ok
        )

    [32] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1147
            [cid] => 220
            [answer] => ok
        )

    [33] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1148
            [cid] => 220
            [answer] => fout
        )

    [34] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1145
            [cid] => 220
            [answer] => ok
        )

    [35] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1146
            [cid] => 220
            [answer] => ok
        )

    [36] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1144
            [cid] => 220
            [answer] => ok
        )

    [37] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1143
            [cid] => 220
            [answer] => ok
        )

    [38] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1142
            [cid] => 220
            [answer] => ok
        )

    [39] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1140
            [cid] => 220
            [answer] => ok
        )

    [40] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1141
            [cid] => 220
            [answer] => ok
        )

    [41] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1139
            [cid] => 220
            [answer] => ok
        )

    [42] => Array
        (
            [infoid] => 230
            [company_id] => 57
            [datum] => 2019
            [tid] => 327
            [catid] => 220
            [info_id] => 230
            [questionid] => 1138
            [cid] => 220
            [answer] => ok
        )

)

我想查看键answer每年对分组的值fout有多少次(datum包含年份,因此在此数组中有两年2019年, 2020)

我如何查看fout在2019年和2020年发生了多少次?

例如,我看到了这个

datum

我尝试在创建数组的循环中进行计数,但由于某种原因,它总是返回0。

这是我之前尝试过的(开始):

2019 - 5 times fout
2020 - 0 times fout

但是while($getwpi = $getwpicon->fetch_assoc()){ $year = date('Y', strtotime($getwpi['datum'])); $getwpi['datum'] = $year; // update your field $wpi[] = $getwpi; // add to the result array $counted = count($wpi['datum']); } 返回0。

这是我当前的循环:

$counted

3 个答案:

答案 0 :(得分:1)

您可以在循环中添加计数:

while($getwpi = $getwpicon->fetch_assoc()){
  $year = date('Y', strtotime($getwpi['datum']));
  $getwpi['datum'] = $year; // update your field

  if ($getwpi['answer'] == "fout") {
      $res[$year] = isset($res[$year]) ? $res[$year] + 1 : 1;
  }
  $wpi[] = $getwpi; // add to the result array
}

现在$res将被排列为具有每年计数的数组。您可以循环播放并打印所需的内容。

答案 1 :(得分:1)

您可以使用array_walkarray_key_exists来解决这个问题

$res=[];
array_walk($arr, function($v, $k) use (&$res){//$arr is the main array
  $res[$v['datum']] = (array_key_exists($v['datum'],$res) && !empty($v['answer'])) ? ($res[$v['datum']]+=1) : 1;
});
echo '<pre>';
print_r($res);

输出示例:

Array
(
  [2019] => 4
)

答案 2 :(得分:0)

尝试此代码:)。

$(document).ready(function(e) {
var theArray=Array({
            infoid: 1,
            company_id: 1,
            datum: 2019,
            tid: "aziz-elanzi",
            catid: "ELANZI",
            answer: "fout"
        },
		{
            infoid: 1,
            company_id: 1,
            datum: 2020,
            tid: "aziz-elanzi",
            catid: "ELANZI",
            answer: "fout"
        },
		{
            infoid: 1,
            company_id: 1,
            datum: 2019,
            tid: "aziz-elanzi",
            catid: "ELANZI",
            answer: "ok"
        },
		{
            infoid: 1,
            company_id: 1,
            datum: 2019,
            tid: "aziz-elanzi",
            catid: "ELANZI",
            answer: "fout"
        },
		{
            infoid: 1,
            company_id: 1,
            datum: 2019,
            tid: "aziz-elanzi",
            catid: "ELANZI",
            answer: "fout"
        },
		{
            infoid: 1,
            company_id: 1,
            datum: 2019,
            tid: "aziz-elanzi",
            catid: "ELANZI",
            answer: "fout"
        },
		{
            infoid: 1,
            company_id: 1,
            datum: 2020,
            tid: "aziz-elanzi",
            catid: "ELANZI",
            answer: "fout"
        });
	var count2019=0;
	var count2020=0;
   theArray.forEach(function(data) {
	   if(data['answer']=='fout'){
			if (data['datum']==2019) count2019+=1;
			else if  (data['datum']==2020) count2020+=1;
	   }
	}); 
	
alert('count2019 : '+count2019+' - count2020 : '+count2020);
	
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>