数据格式
{
"prtg-version": "17.3.33.2753",
"treesize": 3,
"sensors": [
{
"objid": 22216,
"sensor": "Ping",
"status": "正常运行",
"status_raw": 3
},
{
"objid": 22224,
"sensor": "Ping",
"status": "停机",
"status_raw": 5
},
{
"objid": 22226,
"sensor": "Ping",
"status": "已暂停 (由父级暂停)",
"status_raw": 7
}
]
}
项目需求
- 获取总数量;
- 获取"正常运行"状态的个数;
- 获取异常状态的个数(除正常运行外的个数);
项目分析
- 获取总数量,使用count函数即可;
- 获取“正常运行”个数,重点;
- 总数量-正常运行个数;
封装函数
/*02.*筛选关联数组
* $arr,筛选数组
* $column,列名
* $status,筛选值
* */
function getCol($arr, $column, $status)
{
$dataList = array_column($arr, $column);//从记录集中取出status列
$count = array_count_values($dataList);
@$countStatus = $count[$status];
//判断是否正常运行存在;
if ($countStatus === NULL) {
return 0;
} else {
return $countStatus;
}
}
函数调用
/*计算正常情况*/
$reArr = json_decode(getAPI($url), true);
$data = $reArr["sensors"];
$nomal = getCol($data, 'status', "正常运行");
$res['data']["total"] = count($data);
$res['data']["nomal"] = $nomal;
$res['data']["abnormal"] = count($data) - $nomal;
lockdatav Done!