您当前的位置: 首页 >  php

漏刻有时

暂无认证

  • 0浏览

    0关注

    717博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

php清洗数据实战案例(1):根据关联数组的某个状态求不同状态下的个数

漏刻有时 发布时间:2021-03-13 23:15:21 ,浏览量:0

数据格式
{
    "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
        }
    ]
}
项目需求
  1. 获取总数量;
  2. 获取"正常运行"状态的个数;
  3. 获取异常状态的个数(除正常运行外的个数);
项目分析
  1. 获取总数量,使用count函数即可;
  2. 获取“正常运行”个数,重点;
  3. 总数量-正常运行个数;
封装函数
/*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!

关注
打赏
1661217259
查看更多评论
立即登录/注册

微信扫码登录

0.0461s