json multiple arrays decode [duplicate] - php

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;

Related

PHP array_filtered acces object [duplicate]

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

How to get keys from JSON [duplicate]

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();

PHP array return entire section [duplicate]

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

return single array from json by passing a value [duplicate]

This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
How can I get useful error messages in PHP?
(41 answers)
Closed 3 years ago.
I have a json that I need to filter a specific key and value from
the following json
{
"5": {
"Owner": "94EAC",
"Record":"0121ln"
},
"15": {
"Owner": "009AC",
"Record":"0120Pc"
},
"1": {
"Owner": "00G11A",
"Record":"000lPcn"
},
"199": {
"Owner": "00G1y9",
"Record":"01211cn"
},
"33": {
"Owner": "001AC",
"Record":"0121n"
}
}
I would like to be able to pass the first int and get back array for that number.
For example if I pass 15 I get
{
"Owner": "009AC",
"Record":"0120Pc"
}
I tried foreach loop but cannot set specific value for the first int
If I assign $data = json
then $date[15] didn't work
$data->15 also didn't work
I did also use the json decode and was able to print an array but wasn't able to get a single value
Any help would be great, I did spend all day and still cannot get an answer.
Thank you
Using Array:
$arr = json_decode($json, true);
print_r( $arr['15']);
Using Object:
$obj = json_decode($json);
print_r( $obj['15']);
Reference: json_decode

PHP Get value from JSON [duplicate]

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'];

Categories