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.
Related
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 need to sum values of json object using PHP
json
{
"links": [
{
"source": 9887878787,
"target": 9999999993,
"value": 1
},
{
"source": 9999999993,
"target": 9887878787,
"value": 2
}
]
}
Want to Combine value of first and second object to get
desired output
{
"links": [
{
"source": 9887878787,
"target": 9999999993,
"value": 3
},
{
"source": 9999999993,
"target": 9887878787,
"value": 3
}
]
}
How can I achieve this without using javasript.I need php script for that?
Thank you a lot in advance.
Here is a way to do it :
$data = json_decode('{
"links": [
{
"source": 9887878787,
"target": 9999999993,
"value": 1
},
{
"source": 9999999993,
"target": 9887878787,
"value": 2
}
]
}');
$sum = 0;
foreach ($data->links as $link) {
$sum += $link->value;
}
foreach ($data->links as &$link) {
$link->value = $sum;
}
echo json_encode($data);
Hope this helps.
How can I parsing JSON file in PHP with structure like this.
{
"url": "http://lotto.mthai.com",
"result": {
"extractorData": {
"url": "http://lotto.mthai.com",
"resourceId": "85407efc3b77dc0c03350101dbe4d644",
"data": [
{
"group": [
{
"title": [
{
"text": "รางวัลที่ 1"
}
],
"number": [
{
"text": "511825"
}
]
},
{
"title": [
{
"text": "เลขท้าย 2 ตัว"
}
],
"number": [
{
"text": "14"
}
]
},
{
"title": [
{
"text": "รางวัลเลขหน้า 3 ตัว"
}
],
"number": [
{
"text": "111 775"
}
]
},
{
"title": [
{
"text": "รางวัลเลขท้าย 3 ตัว"
}
],
"number": [
{
"text": "880 937"
}
]
},
{
"title": [
{
"text": "รางวัลข้างเคียงรางวัลที่ 1"
}
],
"number": [
{
"text": "511824 511826"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล รางวัลที่ 2"
}
],
"number": [
{
"text": "041316 051696 174632 262383 301461"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล ตรวจหวย รางวัลที่ 3"
}
],
"number": [
{
"text": "151368 192853 218010 381563 441994 593440 608530 958702 960236 991980"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล ตรวจหวย รางวัลที่ 4"
}
],
"number": [
{
"text": "005297 143532 183328 308705 457316 561361 692884 813010 891564 984090 027175 151262 203467 309366 464915 580426 725352 835838 895659 986826 034358 160471 207682 426987 477041 626565 727202 840597 927093 990225 055970 175123 221131 428397 542542 674499 761363 883250 936443 994258 100858 175672 285542 434884 558734 692634 795866 888874 967956 996299"
}
]
},
{
"title": [
{
"text": "ผลสลากกินแบ่งรัฐบาล ตรวจหวย รางวัลที่ 5"
}
],
"number": [
{
"text": "018823 074535 190371 313865 352942 456747 541518 709349 861423 919589 030122 085212 200070 318968 359324 474167 571715 734617 862093 924308 047376 102785 206781 320576 376900 481372 573928 739412 863966 928572 049905 128721 209050 324031 386258 505554 599073 767614 867382 958601 051129 132159 227324 326875 396408 506582 637094 786639 869522 960148 051242 137750 265952 327702 405928 507898 654254 788560 878668 962359 052418 156993 269239 330428 416971 508844 664963 805061 881503 971836 054447 157814 286374 330762 434541 526775 705692 811851 890915 983191 060979 176311 301301 336489 451863 529673 707115 819033 902456 983528 061270 184563 308537 352516 455643 535855 707396 829248 908825 999541"
}
]
}
]
}
]
},
"pageData": {
"statusCode": 200,
"timestamp": 1464855539981
}
}
}
json file exported from import.io
I want to parsing title and number in group and keep it in array.
I tried many ways but always got error.
Thanks you.
Unless I misunderstood your question you could organize your JSON like below:
"group": [
{
"title": "Some text",
"number": "511825"
},
{
"title": "Some text 2",
"number": "98989"
},
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 />";
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);
}