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

时间:2015-11-10 00:57:35

标签: php json sorting field

我想获取一些代码来获取每个唯一客户端ID的列表以及通过该阵列重复客户端ID的频率。

以下是JSON的片段。

如果可能的话,我希望在PHP中使用以下内容。

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

有没有一种简单的方法可以做到这一点?

感谢。

罗布

"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 个答案:

答案 0 :(得分:3)

首先遍历json并构建一个mac地址数组:

$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'];
        }
    }
}

然后您只需使用array_count_values

var_dump(array_count_values($macs));

这将输出一个数组,其中mac地址为键,频率为值:

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

所以你可以做到

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

还有其他方法,例如在循环中你可以检查是否已经看过mac,并且只需在计数中加1即可。但这种方式似乎最简单。

答案 1 :(得分:2)

不确定一种简单的方法,但您可以迭代数据并将您的发现存储在数组中。

//parse json
$data = json_decode( $json );

//store mac addresses
$parsed = array();

foreach( $data['ranges'] as $range ) {
    foreach( $range->clients as $client ) {
        $address = $client->clientId->mac;

        if( ! isset( $parsed[$address] ) )
            $parsed[$address] = 0;

        $parsed[$address]++;
    }
}

//output as requested
foreach( $parsed as $mac => $count )
    echo "Mac {$mac} {$count} <br />";