Created
February 28, 2017 23:47
-
-
Save acapps/c916f789a26a90b599ed433e89583686 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$jsonInput = ' | |
[{ | |
"_id": "58b5c92eb2f1c45f9b7d5bfd", | |
"ap_mac": "80:2a:a8:80:d8:f3", | |
"authorized_by": "api", | |
"end": 1496084526, | |
"mac": "00:26:08:db:ec:d4", | |
"site_id": "58261efcaa83e82c6b230cff", | |
"start": 1488308526 | |
}, | |
{ | |
"_id": "58b040e4b2f1c45f9b7d5bef", | |
"ap_mac": "80:2a:a8:80:d8:f3", | |
"authorized_by": "api", | |
"bytes": 14379698, | |
"channel": 1, | |
"duration": 1795, | |
"end": 1495721956, | |
"ip": "10.1.212.16", | |
"mac": "60:f8:1d:c8:8f:f0", | |
"radio": "ng", | |
"roam_count": 0, | |
"rx_bytes": 4637842, | |
"site_id": "58261efcaa83e82c6b230cff", | |
"start": 1487945956, | |
"tx_bytes": 9741856 | |
}]'; | |
/** | |
* @param $inputArray | |
* @param $searchValue | |
* @return null | |
*/ | |
function retrieveArray($inputArray, $searchValue) { | |
foreach ($inputArray as $item) { | |
if (!array_key_exists('mac', $item)) { | |
continue; | |
} | |
if ($item['mac'] == $searchValue) { | |
return $item; | |
} | |
} | |
return null; | |
} | |
// Parse JSON into associative array. | |
$input = json_decode($jsonInput, true); | |
$searchValue = '60:f8:1d:c8:8f:f0'; | |
print_r(retrieveArray($input, $searchValue)); | |
// Prints: | |
/* | |
Array | |
( | |
[_id] => 58b040e4b2f1c45f9b7d5bef | |
[ap_mac] => 80:2a:a8:80:d8:f3 | |
[authorized_by] => api | |
[bytes] => 14379698 | |
[channel] => 1 | |
[duration] => 1795 | |
[end] => 1495721956 | |
[ip] => 10.1.212.16 | |
[mac] => 60:f8:1d:c8:8f:f0 | |
[radio] => ng | |
[roam_count] => 0 | |
[rx_bytes] => 4637842 | |
[site_id] => 58261efcaa83e82c6b230cff | |
[start] => 1487945956 | |
[tx_bytes] => 9741856 | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment