This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 4 months ago.
Array([0] => {
"data": [{
"id": "91391723276811"
}],
"paging": {
"cursors": {
"before": "QVFIUmZANLWV5RjJ5SFBWQTE4SV9kbkpIbXFETlVDX19SVnRhaTFhTVFEbTdyYWtjUVRHcV82VUlFaEFpWDZA1WE1ORXE0ZAk5MTWhGV2l6djBMOGwxdzFXYnhn",
"after": "QVFIUmZANLWV5RjJ5SFBWQTE4SV9kbkpIbXFETlVDX19SVnRhaTFhTVFEbTdyYWtjUVRHcV82VUlFaEFpWDZA1WE1ORXE0ZAk5MTWhGV2l6djBMOGwxdzFXYnhn"
}
}
})
$host = "https://graph.facebook.com/v15.0/['form_id']/leads? fields=ad_id & access_token="";
$result = file_get_contents($host);
var_dump(json_decode($result, true));
$result = array($result);
print-r($result[0]['data']['id])
I think you are trying to access the id parameter from this data.
{
"data": [{
"id": "91391723276811"
}],
"paging": {
"cursors": {
"before": "QVFIUmZANLWV5RjJ5SFBWQTE4SV9kbkpIbXFETlVDX19SVnRhaTFhTVFEbTdyYWtjUVRHcV82VUlFaEFpWDZA1WE1ORXE0ZAk5MTWhGV2l6djBMOGwxdzFXYnhn",
"after": "QVFIUmZANLWV5RjJ5SFBWQTE4SV9kbkpIbXFETlVDX19SVnRhaTFhTVFEbTdyYWtjUVRHcV82VUlFaEFpWDZA1WE1ORXE0ZAk5MTWhGV2l6djBMOGwxdzFXYnhn"
}
}
}
You can json_decode the data and can access it like data[0]['id'];
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 months ago.
I've been trying to find ways to print the individual data but can't seem to figured out where I'm going wrong.
I started with this but I get no results. Before this I tried nesting loops but also got nowhere either.
$data = curl_exec($ch);
$d = json_decode($data, true);
foreach($d as $k=>$v){
echo $v['value']['displayName'];
}
Then I tried the following with only got me some of the results. I'm not sure where I'm going wrong with this.
foreach(json_decode($data,true) as $d){
foreach($d as $k=>$v){
foreach($v as $kk=>$vv){
echo $kk.$vv;
}
}
}
The JSON looks like the following:
{
"value": [
{
"id": "",
"name": "",
"etag": "",
"type": "Microsoft.SecurityInsights/alertRules",
"kind": "Scheduled",
"properties": {
"incidentConfiguration": {
"createIncident": true,
"groupingConfiguration": {
"enabled": false,
"reopenClosedIncident": false,
"lookbackDuration": "PT5M",
"matchingMethod": "AllEntities",
"groupByEntities": [],
"groupByAlertDetails": null,
"groupByCustomDetails": null
}
},
"entityMappings": [
{
"entityType": "Account",
"fieldMappings": [
{
"identifier": "FullName",
"columnName": "AccountCustomEntity"
}
]
},
{
"entityType": "IP",
"fieldMappings": [
{
"identifier": "Address",
"columnName": "IPCustomEntity"
}
]
}
],
"queryFrequency": "P1D",
"queryPeriod": "P1D",
"triggerOperator": "GreaterThan",
"triggerThreshold": 0,
"severity": "Medium",
"query": "",
"suppressionDuration": "PT1H",
"suppressionEnabled": false,
"tactics": [
"Reconnaissance",
"Discovery"
],
"displayName": "MFA disabled for a user",
"enabled": true,
"description": "Multi-Factor Authentication (MFA) helps prevent credential compromise. This alert identifies when an attempt has been made to diable MFA for a user ",
"alertRuleTemplateName": null,
"lastModifiedUtc": "2022-11-14T02:20:28.8027697Z"
}
},
...
...
...
Here is how you can get the display name without a loop. Notice that the 0 is the key value of the array since it doesn't have a name.
We start from the value, and we move one layer deeper by selecting the first array 0. Now we need to select the properties and finally, we can get the displayName from there.
$displayName = $d["value"][0]["properties"]["displayName"];
echo($displayName);
/*
Here is a quick demonstration:
value:
{
0:
{
...
properties:
{
...
displayName: [We made it!]
}
}
}
*/
And here is a very good post that explains this in more detail
How to decode multi-layers nested JSON String and display in PHP?
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I have json return:
{ "items": [ { "ItemID": 810, "ItemSum": 2 }, { "ItemID": 902, "ItemSum": 5 } ], "es": "", "ec": 0 }
How can I get value ItemSum by ItemID in php ?
How can I get value ItemSum by ItemID in php ?
You need to go through the items and look for the ItemID, e. g.:
$object = json_decode('{ "items": [ { "ItemID": 810, "ItemSum": 2 },
{ "ItemID": 902, "ItemSum": 5 } ], "es": "", "ec": 0 }');
function getSumbyID($object, $ID)
{
foreach ($object->items as $item) if ($item->ItemID == $ID) return $item->ItemSum;
}
echo getSumbyID($object, 902), "\n";
Like so:
$result = json_decode('{ "items": [ { "ItemID": 810, "ItemSum": 2 }, { "ItemID": 902, "ItemSum": 5 } ], "es": "", "ec": 0 }');
foreach($result->items as $item) {
var_dump($item->ItemSum);
}
Also, definitely read what's at the link #Paul Crovella commented with.
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
i'm using this API called "pollDaddy",
using php to retrieve json responses ..I've come across a little hiccup..
How do you get the total of each answer?
My json data:
{
"pdResponse": {
"partnerGUID": "3F2504E0-4F89-11D3-9A0C-0305E82C3301",
"userCode": "123456-FErKS5yu15scpSGmvip4JA==",
"demands": {
"demand": {
"result": {
"answers": {
"answer": [{
"text": "Yes",
"id": "23123124",
"total": "1074",
"percent": "89.13"
}, {
"text": "No",
"id": "23123125",
"total": "131",
"percent": "10.87"
}]
}, "id": "690432265"
}, "id": "GetPollResults"
}
}
}
}
First we have to decode the json data. so we use json_decode. Then we select the right element and loop through it to get all the anwers
$data = '{ "pdResponse": { "partnerGUID": "3F2504E0-4F89-11D3-9A0C-0305E82C3301", "userCode": "123456-FErKS5yu15scpSGmvip4JA==", "demands": { "demand": { "result": { "answers": { "answer": [{ "text": "Yes", "id": "23123124", "total": "1074", "percent": "89.13" }, { "text": "No", "id": "23123125", "total": "131", "percent": "10.87" }] }, "id": "690432265" }, "id": "GetPollResults" } } } }';
$response = json_decode($data);
$answers = $response->pdResponse->demands->demand->result->answers->answer;
foreach($answers as $a)
{
echo $a->total;
}
This question already has answers here:
how to add item to the json file formatted array
(5 answers)
Closed 7 years ago.
I have this JSON array, and i want to add another value to it using PHP.
What would be the easiest way to add a ID and Name to this array using PHP.
[
{
"id":1,
"name":"Charlie"
},
{
"id":2,
"name":"Brown"
},
{
"id":3,
"name":"Subitem",
"children":[
{
"id":4,
"name":"Alfa"
},
{
"id":5,
"name":"Bravo"
}
]
},
{
"id":8,
"name":"James"
}
]
Simply, decode it using json_decode()
And append array to resulting array.
Again encode it using json_encode()
Complete code:
<?php
$arr = '[
{
"id":1,
"name":"Charlie"
},
{
"id":2,
"name":"Brown"
},
{
"id":3,
"name":"Subitem",
"children":[
{
"id":4,
"name":"Alfa"
},
{
"id":5,
"name":"Bravo"
}
]
},
{
"id":8,
"name":"James"
}
]';
$arr = json_decode($arr, TRUE);
$arr[] = ['id' => '9999', 'name' => 'Name'];
$json = json_encode($arr);
echo '<pre>';
print_r($json);
echo '</pre>';
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parsing JSON file with PHP
Can anyone suggest an elegant way to change an array of the form below to an array containing only the primary keys, using php?
[{
"PrimaryKey": "489",
"name": "Ted"
}, {
"PrimaryKey": "488",
"name": "Bill"
}, {
"PrimaryKey": "487",
"singleFbId": "Joe"
}]
<?php
$data = '[{
"PrimaryKey": "489",
"name": "Ted"
}, {
"PrimaryKey": "488",
"name": "Bill"
}, {
"PrimaryKey": "487",
"singleFbId": "Joe"
}]';
$array = json_decode($data, TRUE);
print_r($array);