This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 10 months ago.
Let's say I have this JSON:
{
"achievement": [
{
"title": "Ready for Work",
"description": "Sign up and get validated",
"xp": 50,
"difficulty": 1,
"level_req": 1
},
{
"title": "All Around Submitter",
"description": "Get one piece of textual content approved in all five areas.",
"xp": 500,
"difficulty": 2,
"level_req": 1
}
}
and I am trying this thru PHP:
$string = file_get_contents("achievements.json");
$json_a=json_decode($string,true);
$getit = $json_a->achievement['title'][1];
I'm trying to get the first "id" of the achievement.. which would be READY FOR WORK.
How do I fix this?
When you set the second parameter of json_decode to true, it will return an array.
$json_a=json_decode($string,true);
returns an array.
$getit = $json_a['achievement'][1]['title'];
Related
This question already has answers here:
PHP multidimensional array search by value
(23 answers)
Closed 14 days ago.
I'm trying to filter array from DB and I've got this postman response:
{
"1": {
"id": "3",
"key": "emails_html_body_start",
"value": "value"
}}
How I can access to id, key, value?
My code here:
$start = array_filter($array, function ($var) {
return ($var['key'] == 'emails_html_body_start');
});
echo json_encode($start);
Your question is a bit unclear ... So the upper code is what is sent by the lower code snippet? So the cho json_encode($start); is what produces the upper json data?
If so, then you obviously need to json decode that data again to be able to access a property inside that structure:
<?php
$input = <<<JSON
{
"1": {
"id": "3",
"key": "emails_html_body_start",
"value": "value"
}
}
JSON;
$data = json_decode($input, true);
$output = $data[1]['id'];
print_r($output);
The output obviously is:
3
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 10 months ago.
This post was edited and submitted for review 10 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Please see json below
{
"id": "1",
"name": "john",
"data": {
"1": {
"amount": "9000",
"slip": "/image'/loidsds.jpg"
},
"method": "pump"
}
}
I am trying to read amount and slip how can i manage to do this is PHP ?
I have tried the following
$object->data->1->amount and $object->data->1->slip
I got some errors please help me find an alternative
Decode as an array by passing a truthy value as the second argument to json_decode():
$json = json_decode($string, true);
$amount = $json['data'][1]['amount'];
$slip = $json['data'][1]['slip'];
Or decode as an object, but then you have to brace the 1 because it's not normally a valid attribute name:
$json = json_decode($string);
$amount = $json->data->{1}->amount;
$slip = $json->data->{1}->slip;
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I'm using Laravel. I have JSON returned from Database.
I would like to get key names like "id / name / email / revision" from following JSON.
{
"id": "000",
"records": [
{
"id": 23,
"name": "hoge",
"email": "Hoge#alk.jp",
"revision": 0
},
{
"id": 24,
"name": "zaku",
"email": "zaku#alk.jp",
"revision": 0
},
]
}
Please let me know how to get key names.
Thank you.
You should get the keys with collection get key by code following:
collect($data->records)->keys();
This will return
id
name
email
revision
More detail you can check here: https://laravel.com/docs/8.x/collections#method-keys
Use json_decode to convert it to an array:
$array = josn_decode($jsonVariable, true);
Then use foreach:
foreach($array['records'][0] as $key => $value){
$keys[] = $key;
}
or array_keys:
array_keys($array['records']);
or collect to get keys:
$keys = collect($array['records'])->keys();
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
Hi I am trying to get everything in within the [] brackets. I have tried everything I could find but no luck
{
"Items": [
{
"UID": "XXX-XXX",
"Code": "USD",
"CurrencyName": "US Dollar",
"CurrencyRate": 0.71428000,
"URI": "https://ar2.api.myob.com/accountright/XXX-XXX/GeneralLedger/Currency/XXX-XXX",
"RowVersion": "-6496407278109851648"
}
],
"NextPageLink": null,
"Count": 1
}
EDIT
My Code
$getsbCurrencyDetailsclass = new myobsbfunctions($_SESSION['access_token'],api_key);
$getsbCurrencyDetails = $getsbCurrencyDetailsclass->getResponsenew($cf_uri. "/GeneralLedger/Currency/?" .'$filter' ."=Code%20eq%20'{$docCurrencyType}'");
$getsbCurrencyDetails = json_decode($getsbCurrencyDetails);
$result=$getsbCurrencyDetails['Items'];
//print("<pre>".print_r($result,true)."</pre>");
echo $getsbCurrencyDetails
You need to convert the JSON into an array, then manipulate this array. Assuming the text is in $json:
$array=json_decode($json, true);
$result=$array['Items'];
EDIT
I forgot the second parameter to json_decode
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 6 years ago.
I have the following code:
$json = ' {
"HTML":
[
{
"id": 1,
"name": "HTML",
"match": false
},
{
"id": 2,
"name": "HTML 5",
"match": false
},
{
"id": 3,
"name": "XHTML",
"match": false
}
]
}';
$obj = json_decode($json);
$obj[0][0]->name; // JavaScript: The Definitive Guide
Why do I get the following error?
use object of type stdClass as array
I decode the json correctly, than I say that I want to pick the first object from the array (in this case HTML) and than I want to pick the name of the first one in the array.
What is going wrong?
Your first JSON is object (HTML), which contains an array of another objects. You must call ->HTML[0] (which is first object in your array) and then ->name, which is parameter of your HTML object.
$obj->HTML[0]->name;