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 />";
Related
I am really struggling to create a new array from this response, I don't know where and how to start or to do it:
{
"fruits": [
{
"name": "Bananas",
"pieces": 2469429
},
{
"name": "Oranges",
"pieces": 1576890
},
{
"name": "Lemons",
"pieces": 5645318
}
],
"size": [
{
"size": "5000-10,000 eaters",
"pieces": 17008533
},
{
"size": "100-500 eaters",
"pieces": 23473914
},
{
"size": "10,001+ eaters",
"pieces": 68086139
}
],
"SizeAndFruit": [
{
"size": "100-500 eaters",
"pieces": 226636,
"name": "Bananas"
},
{
"size": "5000-10,000 eaters",
"pieces": 249004,
"name": "Bananas"
},
{
"size": "10,001+ eaters",
"pieces": 727829,
"name": "Bananas"
},
{
"size": "100-500 eaters",
"pieces": 416310,
"name": "Lemons"
},
{
"size": "5000-10,000 eaters",
"pieces": 488711,
"name": "Lemons"
},
{
"size": "10,001+ eaters",
"pieces": 2340652,
"name": "Lemons"
},
{
"size": "100-500 eaters",
"pieces": 217037,
"name": "Oranges"
},
{
"size": "5000-10,000 eaters",
"pieces": 71779,
"name": "Oranges"
},
{
"size": "10,001+ eaters",
"pieces": 201729,
"name": "Oranges"
}
]
}
I want to get this new structure/array:
{
"newArray": [
{
"name": "Bananas",
"totalResults": 2469429,
"sortedResults": [
{
"size": "100-500 eaters",
"pieces": 226636
},
{
"size": "5000-10,000 eaters",
"pieces": 249004
},
{
"size": "10,001+ eaters",
"pieces": 727829
}
]
},
{
"name": "Oranges",
"totalResults": 1576890,
"sortedResults": [
{
"size": "100-500 eaters",
"pieces": 217037
},
{
"size": "5000-10,000 eaters",
"pieces": 71779
},
{
"size": "10,001+ eaters",
"pieces": 201729
}
]
},
{
"name": "Lemons",
"totalResults": 5645318,
"sortedResults": [
{
"size": "100-500 eaters",
"pieces": 416310
},
{
"size": "5000-10,000 eaters",
"pieces": 488711
},
{
"size": "10,001+ eaters",
"pieces": 2340652
}
]
}
]
}
How can I make for each fruit to add all sizes as subarray from the sizeAndFruit as the example above? After that each fruit has to be row in a table, that's why I need to get this new array? Please let me know if you have any suggestions or any extra questions I will be glad to explain more if needed. Thank you very much!
Edit for David:
How about if we always order them by size?
Here is the snippet: pastecode.io/s/t82n19x4 so when the order is like this. So, to order them by size as it is now on David's reply, because if these are mixed then they order as they go instead by 100-500 5001-10000 10,000+ they go 10.000+ 100-500 5001-1000, etc.
Thank you!
The below codes reads your inputs and turns it into your desired output (including sorting by size):
<?php
$inputContents = file_get_contents(__DIR__ . '/input.json');
$inputJson = json_decode($inputContents);
$out = [];
foreach ($inputJson->fruits as $fruit) {
$out[] = [
'name' => $fruit->name,
'totalResults' => $fruit->pieces,
'sortedResults' => [],
];
}
foreach ($inputJson->SizeAndFruit as $entry) {
$newEntry = [
'size' => $entry->size,
'pieces' => $entry->pieces,
];
foreach ($out as &$fruit) {
if ($fruit['name'] === $entry->name) {
$fruit['sortedResults'][] = $newEntry;
}
}
}
/**
* Get a number of the size from the provided string.
*
* #param string $size
*
* #return int|null
*/
function sizeStringToNumber(string $size): ?int
{
$sizes = [
'100-500 eaters' => 100,
'5000-10,000 eaters' => 5000,
'10,001+ eaters' => 10000,
];
if (isset($sizes[$size])) {
return $sizes[$size];
}
throw new \Exception('Unknown size: ' . $size);
}
/**
* Sort the `sortedResults` array by `size` in ascending order.
*/
foreach ($out as &$fruit) {
usort($fruit['sortedResults'], function ($a, $b) {
$aSize = sizeStringToNumber($a['size']);
$bSize = sizeStringToNumber($b['size']);
return $aSize <=> $bSize;
});
}
$outArray = [
'newArray' => $out,
];
$outJson = json_encode($outArray, JSON_PRETTY_PRINT);
print_r($outJson);
Prints as:
{
"newArray": [
{
"name": "Bananas",
"totalResults": 2469429,
"sortedResults": [
{
"size": "100-500 eaters",
"pieces": 226636
},
{
"size": "5000-10,000 eaters",
"pieces": 249004
},
{
"size": "10,001+ eaters",
"pieces": 727829
}
]
},
{
"name": "Oranges",
"totalResults": 1576890,
"sortedResults": [
{
"size": "100-500 eaters",
"pieces": 217037
},
{
"size": "5000-10,000 eaters",
"pieces": 71779
},
{
"size": "10,001+ eaters",
"pieces": 201729
}
]
},
{
"name": "Lemons",
"totalResults": 5645318,
"sortedResults": [
{
"size": "100-500 eaters",
"pieces": 416310
},
{
"size": "5000-10,000 eaters",
"pieces": 488711
},
{
"size": "10,001+ eaters",
"pieces": 2340652
}
]
}
]
}
I am getting an array from an API that varies in number of levels but follows the same basic structure - here is a truncated sample as this particular repsonse is 25K lines:
{
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "Ordinary Income/Expenses"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Rows": {},
"Summary": {
"ColData": [
{
"value": "Gross Profit"
},
{
"value": ""
}
]
},
"type": "Section"
},
{
"Header": {
"ColData": [
{
"value": "Income"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Header": {
"ColData": [
{
"value": "40000 Sales Income",
"id": "31"
},
{
"value": ""
}
]
},
"Rows": {
"Row": [
{
"Rows": {
"Row": [
{
"ColData": [
{
"value": "2022-01-24"
},
{
"value": "Invoice",
"id": "148774"
},
{
"value": "208232"
},
{
"value": "Hyatt:#211102",
"id": "7568"
},
{
"value": "JN",
"id": "4100000000000368107"
},
{
"value": "CAPTIVE AIRE"
},
{
"value": "11000 Accounts Receivable",
"id": "80"
},
{
"value": "38748.00"
},
{
"value": "38748.00"
}
],
"type": "Data"
},
I need to traverse the json, and where there is data in both [Header][ColData][value] AND [Header][ColData][id] extract the value, id (in this snippet "value": "40000 Sales Income", "id": "31") and the data that immediately follows the "value"/"id" in [Rows][Row][Rows][Row][ColData] (in this snippet starting with "ColData": [{"value": "2022-01-24"...)
[Rows][Row][Rows][Row][ColData] will have one to a few hundred subarrays. I can extract the data from the subarrays once they are found - it's just managing the varying depths of the array that is warping my brain.
[Rows][Row][Rows][Summary] can be discarded as well.
I have tried multiple foreach loops - but by time I get 5 or 6 levels deep it gets very confusing. The number of Header sections varies depending on the report type. The [Rows][Row] nesting is multiple layers deep... I'm sure there has to be a better way than nesting foreach loops...
This is what I've come up with. Kindly modify it to meet your need.
$array = json_decode($json, true);
function traverse_some_array($array){
$result = [];
foreach($array['Rows']['Row'] ?? [] as $row){
if( !empty($row['Header']['ColData']) ){
foreach($row['Header']['ColData'] as $coldata){
if( isset($coldata['value'], $coldata['id']) ){
// there is data in both [Header][ColData][value] AND [Header][ColData][id]
// extract the value, id (in this snippet "value": "40000 Sales Income", "id": "31")
$extract_data = [
'value' => $coldata['value'],
'id' => $coldata['id']
];
// the data that immediately follows the "value"/"id" in [Rows][Row][Rows][Row][ColData]
$immediate_coldata = $row['Rows']['Row'] ?? [];
// you can do what ever you want with the results, eg return it or push it to a result array
$result[] = [
'extract_data' => $extract_data,
'immediate_coldata' => $immediate_coldata
];
}else{
// continue traversing the array
$result = array_merge($result, traverse_some_array($row));
}
}
}
}
return $result;
}
print_r(traverse_some_array($array));
I am trying to extract data values from a rather large JSON file via php. The file comes from a USGS server and includes a large series of information that I don't need I am just trying to get the data values and not all of the metadata. I have never had to handle JSON files of this magnitude as I am rather new to their format.
{
"name": "ns1:timeSeriesResponseType",
"declaredType": "org.cuahsi.waterml.TimeSeriesResponseType",
"scope": "javax.xml.bind.JAXBElement$GlobalScope",
"value": {
"queryInfo": {
"queryURL": "http://waterservices.usgs.gov/nwis/iv/format=json&sites=01129500&startDT=2017-11-05T18:00-0500&endDT=2017-11-06T03:00-0500¶meterCd=00060&siteStatus=all",
"criteria": {
"locationParam": "[ALL:01129500]",
"variableParam": "[00060]",
"timeParam": {
"beginDateTime": "2017-11-05T23:00:00.000",
"endDateTime": "2017-11-06T08:00:00.000"
},
"parameter": []
},
"note": [
{
"value": "[ALL:01129500]",
"title": "filter:sites"
},
{
"value": "[mode=RANGE, modifiedSince=null] interval={INTERVAL[2017-11-05T23:00:00.000Z/2017-11-06T08:00:00.000Z]}",
"title": "filter:timeRange"
},
{
"value": "methodIds=[ALL]",
"title": "filter:methodId"
},
{
"value": "2017-11-06T21:03:28.078Z",
"title": "requestDT"
},
{
"value": "efbacfd0-c335-11e7-9d73-6cae8b663fb6",
"title": "requestId"
},
{
"value": "Provisional data are subject to revision. Go to http://waterdata.usgs.gov/nwis/help/?provisional for more information.",
"title": "disclaimer"
},
{
"value": "vaas01",
"title": "server"
}
]
},
"timeSeries": [
{
"sourceInfo": {
"siteName": "CONNECTICUT RIVER AT NORTH STRATFORD, NH",
"siteCode": [
{
"value": "01129500",
"network": "NWIS",
"agencyCode": "USGS"
}
],
"timeZoneInfo": {
"defaultTimeZone": {
"zoneOffset": "-05:00",
"zoneAbbreviation": "EST"
},
"daylightSavingsTimeZone": {
"zoneOffset": "-04:00",
"zoneAbbreviation": "EDT"
},
"siteUsesDaylightSavingsTime": true
},
"geoLocation": {
"geogLocation": {
"srs": "EPSG:4326",
"latitude": 44.74977166,
"longitude": -71.63120018
},
"localSiteXY": []
},
"note": [],
"siteType": [],
"siteProperty": [
{
"value": "ST",
"name": "siteTypeCd"
},
{
"value": "01080101",
"name": "hucCd"
},
{
"value": "33",
"name": "stateCd"
},
{
"value": "33007",
"name": "countyCd"
}
]
},
"variable": {
"variableCode": [
{
"value": "00060",
"network": "NWIS",
"vocabulary": "NWIS:UnitValues",
"variableID": 45807197,
"default": true
}
],
"variableName": "Streamflow, ft³/s",
"variableDescription": "Discharge, cubic feet per second",
"valueType": "Derived Value",
"unit": {
"unitCode": "ft3/s"
},
"options": {
"option": [
{
"name": "Statistic",
"optionCode": "00000"
}
]
},
"note": [],
"noDataValue": -999999,
"variableProperty": [],
"oid": "45807197"
},
"values": [
{
"value": [
{
"value": "2550",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T18:00:00.000-05:00"
},
{
"value": "2550",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T18:15:00.000-05:00"
},
{
"value": "2520",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T18:30:00.000-05:00"
},
{
"value": "2520",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T18:45:00.000-05:00"
},
{
"value": "2520",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T19:00:00.000-05:00"
},
{
"value": "2520",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T19:15:00.000-05:00"
},
{
"value": "2500",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T19:30:00.000-05:00"
},
{
"value": "2500",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T19:45:00.000-05:00"
},
{
"value": "2500",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T20:00:00.000-05:00"
},
{
"value": "2500",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T20:15:00.000-05:00"
},
{
"value": "2500",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T20:30:00.000-05:00"
},
{
"value": "2490",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T20:45:00.000-05:00"
},
{
"value": "2490",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T21:00:00.000-05:00"
},
{
"value": "2490",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T21:15:00.000-05:00"
},
{
"value": "2490",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T21:30:00.000-05:00"
},
{
"value": "2490",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T21:45:00.000-05:00"
},
{
"value": "2470",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T22:00:00.000-05:00"
},
{
"value": "2470",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T22:15:00.000-05:00"
},
{
"value": "2450",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T22:30:00.000-05:00"
},
{
"value": "2450",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T22:45:00.000-05:00"
},
{
"value": "2450",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T23:00:00.000-05:00"
},
{
"value": "2450",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T23:15:00.000-05:00"
},
{
"value": "2450",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T23:30:00.000-05:00"
},
{
"value": "2450",
"qualifiers": [
"P"
],
"dateTime": "2017-11-05T23:45:00.000-05:00"
},
{
"value": "2450",
"qualifiers": [
"P"
],
"dateTime": "2017-11-06T00:00:00.000-05:00"
},
{
"value": "2470",
"qualifiers": [
"P"
],
"dateTime": "2017-11-06T00:15:00.000-05:00"
},
{
"value": "2470",
"qualifiers": [
"P"
],
"dateTime": "2017-11-06T00:30:00.000-05:00"
},
{
"value": "2470",
"qualifiers": [
"P"
],
"dateTime": "2017-11-06T00:45:00.000-05:00"
},
{
"value": "2450",
"qualifiers": [
"P"
],
"dateTime": "2017-11-06T01:00:00.000-05:00"
},
{
"value": "2450",
"qualifiers": [
"P"
],
"dateTime": "2017-11-06T01:15:00.000-05:00"
},
{
"value": "2470",
"qualifiers": [
"P"
],
"dateTime": "2017-11-06T01:30:00.000-05:00"
},
{
"value": "2470",
"qualifiers": [
"P"
],
"dateTime": "2017-11-06T01:45:00.000-05:00"
},
{
"value": "2470",
"qualifiers": [
"P"
],
"dateTime": "2017-11-06T02:00:00.000-05:00"
},
{
"value": "2490",
"qualifiers": [
"P"
],
"dateTime": "2017-11-06T02:15:00.000-05:00"
},
{
"value": "2490",
"qualifiers": [
"P"
],
"dateTime": "2017-11-06T02:30:00.000-05:00"
},
{
"value": "2500",
"qualifiers": [
"P"
],
"dateTime": "2017-11-06T02:45:00.000-05:00"
},
{
"value": "2500",
"qualifiers": [
"P"
],
"dateTime": "2017-11-06T03:00:00.000-05:00"
}
],
"qualifier": [
{
"qualifierCode": "P",
"qualifierDescription": "Provisional data subject to revision.",
"qualifierID": 0,
"network": "NWIS",
"vocabulary": "uv_rmk_cd"
}
],
"qualityControlLevel": [],
"method": [
{
"methodDescription": "",
"methodID": 66577
}
],
"source": [],
"offset": [],
"sample": [],
"censorCode": []
}
],
"name": "USGS:01129500:00060:00000"
}
]
},
"nil": false,
"globalScope": true,
"typeSubstituted": false
}
How do I grab all of the information with the label "value" and store these numbers into a data structure I can then perform functions on?
Thanks
That question has nothing to do with busy parsing and the json itself isn't that large. You could just iterate over $data['value']['timeSeries'][0]['values'][0]['value'] and extract everything you need.
$data = json_decode($json, true);
$t = [];
foreach ($data['value']['timeSeries'][0]['values'][0]['value'] as $item) {
$t[] = $item['value'];
}
If you need different keys, just have a look at the json data and modify the loop. If you need unique keys, use array_unique on the $t var.
The naive thing to do is a simple recursive operation:
$arr = json_decode($json,true);
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($arr), RecursiveIteratorIterator::SELF_FIRST);
$values = [];
foreach ( $iterator as $key => $value ) {
if ($key === "value" && !is_array($value)) {
$values[] = $value;
}
}
Example: http://sandbox.onlinephpfunctions.com/code/b36d70d84926d9b5ca9989c9ba96b34fe1f2a7b6
I am trying to display a list in PHP from a JSON API feed, and the fields keep changing from one array to the next.
Can someone help with a solution to get the display right
Json script, you will see that the JSON has both uniqueId and mac fields that change in each instance under the clientId
{
"searchMeta": {
"maxResults": 100,
"sourceId": "6e5c1d5d-d84b-4b64-9d2c-a32b8c9f7174",
"iso8601Timestamps": true,
"sourceType": "VENUE",
"endTimestamp": 1444952156,
"startTimestamp": 1444952056
},
"ranges": [
{
"clients": [
{
"clientId": {
"mac": "6c:19:8f:bf:47:e9"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -93.3
}
]
},
{
"clientId": {
"uniqueId": "Kn04"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -75.3
}
]
},
{
"clientId": {
"mac": "58:6d:8f:75:95:0e"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -86.2
}
]
},
{
"clientId": {
"mac": "44:d9:e7:21:e0:de"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -25.8
}
]
},
{
"clientId": {
"mac": "68:72:51:10:e7:26"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -47
}
]
},
{
"clientId": {
"mac": "68:72:51:10:e7:29"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -72.3
}
]
},
{
"clientId": {
"mac": "a4:ee:57:2e:ac:bd"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -95
}
]
},
{
"clientId": {
"uniqueId": "CQos"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -64.1
}
]
},
{
"clientId": {
"mac": "86:8f:c2:8f:c3:20"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -68.4
}
]
},
{
"clientId": {
"mac": "32:91:8f:6c:2e:f4"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -87.7
}
]
},
{
"clientId": {
"mac": "30:91:8f:6c:2e:f3"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -86.9
}
]
},
{
"clientId": {
"mac": "30:91:8f:43:ca:49"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -87
}
]
},
{
"clientId": {
"mac": "1d:8b:90:7b:20:9c"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -102.5
}
]
},
{
"clientId": {
"mac": "38:2c:4a:5c:b6:a0"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -76.7
}
]
},
{
"clientId": {
"uniqueId": "ECgg"
},
"rssis": [
{
"sourceId": "zR1L3",
"value": -59.5
}
]
}
],
"timestamp": "2015-10-15T23:35:00+00:00"
}
]
}
And then the code to read and display it is
$json = json_decode($response,true);
$output = "<ul>";
foreach($json['ranges'] as $range){
foreach($range['clients'] as $client)
foreach($client['clientId'] as $client1)
foreach($client['rssis'] as $data){
$output .= "<h4>".$range['timestamp']."</h4>";
$output .= "<li>Source ID: ".$data["sourceId"]."</li>";
$output .= "<li>Value in dB: ".$data["value"]."</li>";
}}
$output .= "</ul>";
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $output;
}
it then displays on the webpage leaving out the uniqueId or the mac fields. I need the line that will display either of these please?
2015-10-13T04:26:00+00:00
Source ID: zR1L3 Value in dB: -89.2
2015-10-13T04:26:00+00:00
Source ID: zR1L3 Value in dB: -98.8
2015-10-13T04:26:00+00:00
Source ID: zR1L3 Value in dB: -74.6
Thanks for the answer below, now I get the following.
2015-10-16T10:11:00+00:00
2015-10-16T10:11:00+00:00
2015-10-16T10:11:00+00:00
2015-10-16T10:11:00+00:00
2015-10-16T10:11:00+00:00
2015-10-16T10:11:00+00:00
2015-10-16T10:11:00+00:00
2015-10-16T10:11:00+00:00
2015-10-16T10:11:00+00:00
2015-10-16T10:11:00+00:00
2015-10-16T10:11:00+00:00
2015-10-16T10:11:00+00:00
2015-10-16T10:11:00+00:00
mac : f4:b7:e2:31:65:8f
sourceId : zR1L3
value : -95
2015-10-16T10:12:00+00:00
2015-10-16T10:12:00+00:00
2015-10-16T10:12:00+00:00
2015-10-16T10:12:00+00:00
2015-10-16T10:12:00+00:00
mac : f4:b7:e2:31:65:8f
sourceId : zR1L3
value : -95
2015-10-16T10:13:00+00:00
Not getting what exactly you are asking. If i have understood correctly, you want to access few fields from json encoded array.
Try this
$json = json_decode($response,true);
foreach($json->ranges as $row){
foreach($row->clients as $row1)
echo $row->timestamp."<br>";
foreach($row1->clientId as $key => $val){
echo $key." ==== ". $val."<br>";
}
foreach($row1->rssis as $row2){
foreach($row2 as $key => $val){
echo $key." ==== ". $val."<br>";
}
}
}
I could not get timestamp from what you have posted as json array.
I'm trying to retrieve the responses for a particular survey from Survey Monkey using the get_responses endpoint which only retrieves the "col" and "row" ids.
{
"status": 0,
"data": [
{
"respondent_id": "4165059283",
"questions": [
{
"answers": [
{
"text": "04/11/1990",
"row": "9333771686"
}
],
"question_id": "842633241"
},
{
"answers": [
{
"row": "9333771693"
},
{
"text": "aa",
"row": "9333771691"
}
],
"question_id": "842633242"
},
{
"answers": [
{
"col": "9333771714",
"row": "9333771713"
},
{
"col": "9333771717",
"row": "9333771712"
},
{
"col": "9333771714",
"row": "9333771711"
},
{
"col": "9333771717",
"row": "9333771710"
},
{
"col": "9333771717",
"row": "9333771709"
},
{
"col": "9333771716",
"row": "9333771708"
},
{
"col": "9333771714",
"row": "9333771707"
},
{
"col": "9333771716",
"row": "9333771706"
},
{
"col": "9333771716",
"row": "9333771705"
},
{
"col": "9333771715",
"row": "9333771704"
},
{
"col": "9333771715",
"row": "9333771703"
},
{
"col": "9333771714",
"row": "9333771702"
},
{
"text": "aa",
"row": "0"
}
],
"question_id": "842633243"
},
{
"answers": [
{
"col": "9333771729",
"row": "9333771726"
},
{
"col": "9333771727",
"row": "9333771725"
},
{
"col": "9333771727",
"row": "9333771724"
},
{
"text": "aaa",
"row": "0"
}
],
"question_id": "842633244"
},
]
}
]
}
I can see that these id's correlate to a question and answer from the get_survey_details endpoint..
But how on earth do you filter the get_survey_details response to only show the question/answer that the respondent has given rather than retrieve everything? I've tried adding to the $params in the class as shown but the request still gives everything back... so clearly this isn't possible or I'm going about it in completely the wrong way:
public function getSurveyDetails($surveyId){
//original $params
//$params = array('survey_id'=>$surveyId);
$params = array(
'survey_id'=>$surveyId,
'pages'=>array(
'questions' => array (
'answers' => array (
'answer_id' => '9333771686',
)
),
)
);
return $this->run('surveys/get_survey_details', $params);
}