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"
},
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.
Not sure about an easy way, but you can iterate over the data and store your findings in an array.
//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 />";