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>';
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'];
I am writing a php file for an application that allows the user to print a project report (with fpdf library).
The aim is to get datas from a db and then to build the PDF file dynamically.
The datas are stored in json.
It is ok for almost all datas, but there are some that I can't reach.
Here is an example :
First of all I already did this :
$Array['roof_coordinates'] = json_decode($Array['roof_coordinates'], false);
To get the number "nb" of "A523500" I'm doing like that :
$Array['roof_coordinates']->results->packingList->total->A523500->nb;
What am I doing wrong ?
"roofCoordinates": {
"results": {
"packingList": {
"total": {
"A523500": {
"ref": "STRA523500",
"nb": 16
},
"A523120": {
"ref": "STRA523120",
"nb": 0
},
"A522100": {
"ref": "STRA522100",
"nb": 8
},
},
}
},
}
And I tried to pass "true" to json_decode to convert objects to associative array but it doesn't seems to work...
Any help will be great !
Thank you in advance ;)
Make sure you are accessing the resulting data structure correctly, it should work whether you decode to an array or an object.
<?php
$json = <<<END
{
"roofCoordinates": {
"results": {
"packingList": {
"total": {
"A523500": {
"ref": "STRA523500",
"nb": 16
},
"A523120": {
"ref": "STRA523120",
"nb": 0
},
"A522100": {
"ref": "STRA522100",
"nb": 8
}
}
}
}
}
}
END;
$arrayResults = json_decode($json, true);
$nbFromArray = $arrayResults['roofCoordinates']['results']['packingList']['total']['A523500']['nb'];
$stdClassResults = json_decode($json);
$nbFromStdClass = $stdClassResults->roofCoordinates->results->packingList->total->A523500->nb;
assert($nbFromArray==16, 'Value should equal 16');
assert($nbFromArray==$nbFromStdClass, 'Values from either json_decode method should be equal');
echo 'From array: '.$nbFromArray.PHP_EOL;
echo 'From stdClass: '.$nbFromStdClass.PHP_EOL;
I have data type like below. But I want to get the 'errors' key and 'errors.message' value.
[
{
"payout_item_id":"HIDDEN",
"transaction_id":"HIDDEN",
"transaction_status":"UNCLAIMED",
"payout_item_fee":{
"currency":"USD",
"value":"10.25"
},
"payout_batch_id":"HIDDEN",
"payout_item":{
"amount":{
"currency":"USD",
"value":"10.00"
},
"note":"Thanks for your patronage",
"receiver":"HIDDEN#gmail.com",
"recipient_type":"EMAIL",
"sender_item_id":"1510369638"
},
"time_processed":"2017-11-11T03:07:26Z",
"errors":{
"name":"RECEIVER_UNCONFIRMED",
"message":"Receiver is unconfirmed",
"information_link":"https:\/\/developer.paypal.com\/docs\/api\/payments.payouts-batch\/#errors"
},
"links":[
{
"href":"https:\/\/api.sandbox.paypal.com\/v1\/payments\/payouts-item\/HIDDEN",
"rel":"item",
"method":"GET"
}
]
}
]
I have tried to store the code in variable ($get_errors) and then call 'errors' key like this. but the code is not working.
return $get_errors['errors'];
return $get_errors->errors;
Thanks.
Try to convert your JSON data to array then fetch the desired result from the array Try the following maybe it's work for you
$getError = '[
{
"payout_item_id":"HIDDEN",
"transaction_id":"HIDDEN",
"transaction_status":"UNCLAIMED",
"payout_item_fee":{
"currency":"USD",
"value":"10.25"
},
"payout_batch_id":"HIDDEN",
"payout_item":{
"amount":{
"currency":"USD",
"value":"10.00"
},
"note":"Thanks for your patronage",
"receiver":"HIDDEN#gmail.com",
"recipient_type":"EMAIL",
"sender_item_id":"1510369638"
},
"time_processed":"2017-11-11T03:07:26Z",
"errors":{
"name":"RECEIVER_UNCONFIRMED",
"message":"Receiver is unconfirmed",
"information_link":"https:\/\/developer.paypal.com\/docs\/api\/payments.payouts-batch\/#errors"
},
"links":[
{
"href":"https:\/\/api.sandbox.paypal.com\/v1\/payments\/payouts-item\/HIDDEN",
"rel":"item",
"method":"GET"
}
]
}
]';
$array_getError = json_decode($getError,true);
var_dump($array_getError[0]["errors"]);
This is Json type. So you have to decode json. Then you can geta data from it
$json = '[
{
"payout_item_id":"HIDDEN",
"transaction_id":"HIDDEN",
"transaction_status":"UNCLAIMED",
"payout_item_fee":{
"currency":"USD",
"value":"10.25"
},
"payout_batch_id":"HIDDEN",
"payout_item":{
"amount":{
"currency":"USD",
"value":"10.00"
},
"note":"Thanks for your patronage",
"receiver":"HIDDEN#gmail.com",
"recipient_type":"EMAIL",
"sender_item_id":"1510369638"
},
"time_processed":"2017-11-11T03:07:26Z",
"errors":{
"name":"RECEIVER_UNCONFIRMED",
"message":"Receiver is unconfirmed",
"information_link":"https:\/\/developer.paypal.com\/docs\/api\/payments.payouts-batch\/#errors"
},
"links":[
{
"href":"https:\/\/api.sandbox.paypal.com\/v1\/payments\/payouts-item\/HIDDEN",
"rel":"item",
"method":"GET"
}
]
}
]';
$data = json_decode($json,true);
echo $data[0]['errors']['message'];
DEMO
$x = '[
{
"payout_item_id":"HIDDEN",
"transaction_id":"HIDDEN",
"transaction_status":"UNCLAIMED",
"payout_item_fee":{
"currency":"USD",
"value":"10.25"
},
"payout_batch_id":"HIDDEN",
"payout_item":{
"amount":{
"currency":"USD",
"value":"10.00"
},
"note":"Thanks for your patronage",
"receiver":"HIDDEN#gmail.com",
"recipient_type":"EMAIL",
"sender_item_id":"1510369638"
},
"time_processed":"2017-11-11T03:07:26Z",
"errors":{
"name":"RECEIVER_UNCONFIRMED",
"message":"Receiver is unconfirmed",
"information_link":"https:\/\/developer.paypal.com\/docs\/api\/payments.payouts-batch\/#errors"
},
"links":[
{
"href":"https:\/\/api.sandbox.paypal.com\/v1\/payments\/payouts-item\/HIDDEN",
"rel":"item",
"method":"GET"
}
]
}
]';
$errors = (json_decode($x)[0]->errors);
This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
How can I access an array/object?
(6 answers)
Closed 5 years ago.
I have a string that looks like this:
[
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264307167",
"quantity":"1",
"colours":"Green",
"pid":"234234234"
},
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264311757",
"quantity":"1",
"colours":"Blue",
"pid":"234234234"
}
]
I need to get the id from this JSON string using PHP.
So I tried this:
$details = '[
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264307167",
"quantity":"1",
"colours":"Green",
"pid":"234234234"
},
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264311757",
"quantity":"1",
"colours":"Blue",
"pid":"234234234"
}
]';
$details = json_encode($details, true);
$array = json_decode($details, true);
$oid = $array['id'];
echo $oid;
The code above is in a while loop so the echo $oid should echo the id
multiple times.
anyway, the code above only prints this:
[
[
and when i look in the error log, i see this error:
PHP Warning: Illegal string offset 'id'
Could someone please advice on this issue?
Thanks in advance.
<?php
$details = '[
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264307167",
"quantity":"1",
"colours":"Green",
"pid":"234234234"
},
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264311757",
"quantity":"1",
"colours":"Blue",
"pid":"234234234"
}
]';
$array = json_decode($details, true);
for($i=0;$i<count($array);$i++){
$idValue = $array[$i]['id'];
echo $idValue;
}
You need a loop to go through all the nested arrays and get all the ids.
Try the following code:
<?php
$details = '[
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264307167",
"quantity":"1",
"colours":"Green",
"pid":"234234234"
},
{
"id":"2",
"price":"39.99",
"timeStamp":"1506264311757",
"quantity":"1",
"colours":"Blue",
"pid":"234234234"
}
]';
$array = json_decode($details, true);
$oid = $array[0]['id'];
echo $oid;
Instead of encoding json string into json. Decode it to PHP array directly. And use [0] index to get first Json Object
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);