2015-11-10 97 views
1

我想獲取一些代碼以獲取每個唯一客戶端ID的列表以及客戶端ID在該數組中重複的頻率。每個JSON字段在數組中重複的次數php

下面是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" 
    }, 
+0

您應該澄清一下您的問題。你想要的mac地址?伯爵?只有獨特的?他們全部?你想PHP來處理這個? – Chris

回答

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,隨着你的進步計數。但這種方式似乎最簡單。

+0

輝煌。非常感謝@rjdown。完美工作。 –

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 />"; 
+0

非常好。 if(!isset($ parsed [$ address]))$ parsed [$ address] = 0; $ parsed [$ address] ++;'相當於'$ parsed [$ address] = ++ $ parsed [$ address] || 0;'? –

+0

@SalvoF不,它會生成一個未定義的索引通知,並將該值設置爲true ... – rjdown

相關問題