douzhu7507 2015-11-10 00:57
浏览 88
已采纳

每个JSON字段在数组php中重复的次数

I am wanting to get some code to get a list of each unique client ID and how often that client ID is repeated through the array.

Below is a snippet of the JSON.

What I would like is something like the following please in PHP if possible.

Mac 12:12:12:12:12 20 times
Mac 23:23:23:23:23 15 times
Mac 34:34:34:34:34 2 times

Is there an easy way to do this please?

Thanks.

Rob

"ranges": [
    {
      "clients": [
        {
          "clientId": {
            "mac": "86:8f:c2:8f:c3:20"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -90.4
            }
          ]
        },
        {
          "clientId": {
            "mac": "6c:19:8f:bf:47:e9"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -91.3
            }
          ]
        },
        {
          "clientId": {
            "mac": "58:6d:8f:75:95:0e"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -86.3
            }
          ]
        },
        {
          "clientId": {
            "mac": "68:72:51:10:e7:26"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -53.7
            }
          ]
        },
        {
          "clientId": {
            "mac": "38:2c:4a:5c:b6:a0"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -87.1
            }
          ]
        },
        {
          "clientId": {
            "mac": "68:72:51:10:e7:29"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -76.9
            }
          ]
        },
        {
          "clientId": {
            "uniqueId": "CQos"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -70.2
            }
          ]
        },
        {
          "clientId": {
            "mac": "a4:ee:57:2e:ac:bd"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -95
            }
          ]
        },
        {
          "clientId": {
            "uniqueId": "ECgg"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -75.4
            }
          ]
        },
        {
          "clientId": {
            "mac": "58:6d:8f:74:bf:f9"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -70
            }
          ]
        }
      ],
      "timestamp": "2015-11-09T22:06:00+00:00"
    },
    {
      "clients": [
        {
          "clientId": {
            "mac": "86:8f:c2:8f:c3:20"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -93
            }
          ]
        },
        {
          "clientId": {
            "mac": "6c:19:8f:bf:47:e9"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -90.8
            }
          ]
        },
        {
          "clientId": {
            "mac": "58:6d:8f:75:95:0e"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -87.2
            }
          ]
        },
        {
          "clientId": {
            "mac": "68:72:51:10:e7:26"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -54.1
            }
          ]
        },
        {
          "clientId": {
            "mac": "38:2c:4a:5c:b6:a0"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -87
            }
          ]
        },
        {
          "clientId": {
            "mac": "68:72:51:10:e7:29"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -77.2
            }
          ]
        },
        {
          "clientId": {
            "uniqueId": "CQos"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -70.8
            }
          ]
        },
        {
          "clientId": {
            "mac": "a4:ee:57:2e:ac:bd"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -95
            }
          ]
        },
        {
          "clientId": {
            "uniqueId": "ECgg"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -72.8
            }
          ]
        },
        {
          "clientId": {
            "mac": "58:6d:8f:74:bf:f9"
          },
          "rssis": [
            {
              "sourceId": "zR1L3",
              "value": -70
            }
          ]
        }
      ],
      "timestamp": "2015-11-09T22:07:00+00:00"
    },
  • 写回答

2条回答 默认 最新

  • dtmjl4427 2015-11-10 01:13
    关注

    First loop through the json and build an array of mac addresses:

    $data = json_decode($json, true);
    $macs = array();
    
    foreach ($data['ranges'] as $range) {
        foreach ($range['clients'] as $client) {
            // check if the client has a mac address, and add it to the array
            if (isset($client['clientId']['mac'])) {
                $macs[] = $client['clientId']['mac'];
            }
        }
    }
    

    Then you can simply use array_count_values:

    var_dump(array_count_values($macs));

    This will output an array with the mac address as the key, and the frequency as the value:

    array (size=8)
      '86:8f:c2:8f:c3:20' => int 2
      '6c:19:8f:bf:47:e9' => int 2
      '58:6d:8f:75:95:0e' => int 2
      '68:72:51:10:e7:26' => int 2
      '38:2c:4a:5c:b6:a0' => int 2
      '68:72:51:10:e7:29' => int 2
      'a4:ee:57:2e:ac:bd' => int 2
      '58:6d:8f:74:bf:f9' => int 2
    

    So you can just do

    foreach (array_count_values($macs) as $mac => $frequency)
    {
        echo "Mac {$mac} {$frequency} times<br/>";
    }
    

    There are other ways, for example in the loop you could check if the mac has been seen already, and just add 1 to a count as you go along. But this way seems simplest.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错