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);
Related
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'];
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 3 years ago.
i have json data like this decode from API, i get with PHP
{
"0": {
"id_siswa": "14477",
"rombel": "15",
"nama_lengkap": "Cahyo"
},
"1": {
"id_siswa": "14484",
"rombel": "15",
"nama_lengkap": "Bowo"
},
"2": {
"id_siswa": "14485",
"rombel": "13",
"nama_lengkap": "Agus Sugiharto"
}
}
but when i call
$data[0]->id_siswa
Uncaught Error: Cannot use object of type stdClass as array
then i try to call like
$data->id_siswa
error : Undefined property: stdClass::$id_siswa in
You need to decode json to get array value. you will get id_siswa output.
$json = '{
"0": {
"id_siswa": "14477",
"rombel": "15",
"nama_lengkap": "Cahyo"
},
"1": {
"id_siswa": "14484",
"rombel": "15",
"nama_lengkap": "Bowo"
},
"2": {
"id_siswa": "14485",
"rombel": "13",
"nama_lengkap": "Agus Sugiharto"
}
}';
echo "<pre>";
$result = json_decode($json,true);
print_r($result);
echo $result[0]['id_siswa'];
you can use json_decode to convert json into array and get the corresponding values with $data[0]['id_siswa'] like this.
$result = json_decode($json,true);
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 a json result from an URL:
{
"result": [{
"user.name": "Spider Man",
"user": "a4ac7bfe6f581640a62d3c31be3ee4dc"
}, {
"user.name": "Bat Man",
"user": "af406b85e4b13500b95fa1eeac1ce626"
}, {
"user.name": "Iron Man",
"user": "18ed9aba4ffb07006979ab6ba110c757"
}, {
"user.name": "Ant Man",
"user": "877a503a98cc4200b95f526ea1ece471"
}, {
"user.name": "Captain America",
"user": "8ec0d9634f2f22004b19ca1f0310c791"
}]
}
How to create a foreach loop to get the following output:
Spider Man
Bat Man
Iron Man
Ant Man
Captain America
And the same for the user-values in the json.
I tried following code but I can't figure it out:
$json = file_get_contents($queryURL, false, $context) or die ("keine verbindung");
$array = json_decode($json, true);
foreach($array as $data) {
$data->user;
}
Thanks for your help.
You decoded as an array not an object. Also, there is another level result:
foreach($array['result'] as $data) {
echo $data['user'];
}
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>';