How can I read this nested json in php? [duplicate] - php

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 this JSON
{
"AED_USD": {
"2019-08-01": 0.272242
}
}
When dumping this as an array I have:
array(1) { ["AED_USD"]=> array(1) { ["2019-08-01"]=> float(0.272242) } }
I am trying to reach get the float value of 0.272242.
How do I get this value in PHP?

You have to json_decode the string with true as second parameter to make it an array, and then just echo what you want using the name of the keys you have. Like so:
$string = '{
"AED_USD": {
"2019-08-01": 0.272242
}
}';
$decode = json_decode($string, true);
echo $decode['AED_USD']['2019-08-01'];

To get the value in PHP, you can do
$value = $yourArray['AED_USD']['2019-08-01'];
If you want more information: https://www.php.net/manual/pt_BR/language.types.array.php

Related

How to extract a series of variables from string of data in php? [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 3 years ago.
The following chunk of data is produced by a webpage along with other html data;
"search":{"searchResults":{"results":[
{"id":"123","name":"ABC","rating":{"average":0,"count":2,"__typename":"Rating"},"category":"AAA/Cars","__typename":"ProductQuery"},
{"id":"456","name":"DEF","rating":{"average":5,"count":8,"__typename":"Rating"},"category":"BBB/Bikes","__typename":"ProductQuery"}
{"id": //and so on//
"}
]}}
How to extract multiple variables from this string like data like "id", "rating" etc., to be able to print it on another php page?
You can use json_decode to convert JSON to the array and then iterate through the array
$json = '{
"search":
{
"searchResults":
{
"results":[
{"id":"123","name":"ABC","rating":{"average":0,"count":2,"__typename":"Rating"},"category":"AAA/Cars","__typename":"ProductQuery"},
{"id":"456","name":"DEF","rating":{"average":5,"count":8,"__typename":"Rating"},"category":"BBB/Bikes","__typename":"ProductQuery"}
]
}
}
}';
$jsonToArray = json_decode($json,true);
foreach($jsonToArray['search']['searchResults']['results'] as $v){
echo $v['id'];
print_r($v['rating']);echo '<br/>';
}

loop json with no array php [duplicate]

This question already has answers here:
Parsing JSON object in PHP using json_decode
(7 answers)
Closed 4 years ago.
I have json like below, how can i loop each id
{
"id1": {
"AreaN": "\u30ef\u30fc\u30eb\u30c9\u30d0\u30b6\u30fc\u30eb",
"AreaM": "\uff9c\uff70\uff99\uff84\uff9e\uff8a\uff9e\uff7b\uff9e\uff70\uff99"
},
"id2": {
"AreaN": "\u30ef\u30fc\u30eb\u30c9\u30d0\u30b6\u30fc\u30eb",
"AreaM": "\uff9c\uff70\uff99\uff84\uff9e\uff8a\uff9e\uff7b\uff9e\uff70\uff99"
}
}
I tried but it does not get all the ids, i cant change the json.
$json = json_decode($doc);
if( count($json) > 0 )
{
foreach($json as $area)
{
}
}
In my case the ids i want to get are not inside an array. Is it possible to loop the ids and keep them as objects, i prefer object access than using square brackets.
$json = json_decode($doc, 1);
foreach ($json as $id => $area) {
// $id is "id1", "id2"
// $area is AreaN & AreaM
}
the 2nd param for json_decode if true will return as array, otherwise will be object

Create array with only tag - Json decode [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 years ago.
How do I return only all the urls item "webformatURL" using json decode?
{
"totalHits":500,
"hits":[
{
"previewHeight":84,
"likes":37,
"favorites":42,
"tags":"yellow, natural, flower",
"webformatHeight":360,
"views":11218,
"webformatWidth":640,
"previewWidth":150,
"comments":8,
"downloads":6502,
"pageURL":"https://example.com/en/yellow-natural-flower-715540/",
"previewURL":"https://example.com/static/uploads/photo/2015/04/10/00/41/yellow-715540_150.jpg",
"webformatURL":"https://example.com/get/ee34b40a2cf41c2ad65a5854e4484e90e371ffd41db413419cf3c271a6_640.jpg",
"imageWidth":3020,
"user_id":916237,
"user":"916237",
"type":"photo",
"id":715540,
"userImageURL":"https://example.com/static/uploads/user/2015/04/07/14-10-15-590_250x250.jpg",
"imageHeight":1703
},
],
"total":7839
}
I try:
$test= json_decode($json);
$result= $test->webformatURL;
Error: warning-undefined-property-stdclass::webformatURL
I've read the manual but I doubt then I would see an example in practice
json_decode
Thanks for any help
Did you mean?
$result = $test->hits[0]->webformatURL;
If you want to extract only this field from the object, you can do:
$urls = [];
foreach($test->hits as $hit) {
$urls[] = $hit->webformatURL;
}
var_dupm($urls);

Json php decoding issue [duplicate]

This question already has answers here:
Read Json/Array string in Php
(2 answers)
Closed 8 years ago.
In fact Im having some troubles with php script that i have made recently. The problem is that I can't get data from a json file damo.json using php. This is the code of the json file:
{ "checkouts":[
{
"billing_address":{
"country":"Italy",
"first_name":"christian"
}
},
{
"billing_address":{
"country":"Italy",
"first_name":"christian"
}
}
]
}
i want to get the first_name record. Is it possible using php ?
this is the php code:
<?php
$data = file_get_contents('demo.json');
$obj = json_decode($data);
foreach ($obj->billing_address as $result)
{
echo $result->name;
}
?>
After you fix your syntax as noted above load the file and use json_decode with first parameter the json and second parameter true for associative array.
$data = file_get_contents( "demo.json" );
$data = json_decode($data, true);
var_dump($data);//you have assoc array

Read JSON Data Using PHP [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 months ago.
Solr returns response in following JSON format.
{
"responseHeader":{
"status":0,
"QTime":2,
"params":{
"indent":"on",
"start":"0",
"q":"*:*",
"wt":"json",
"version":"2.2",
"rows":"10"}},
"response":{"numFound":3,"start":0,"docs":[
{
"student_id":"AB1001",
"student_name":[
"John"]
},
{
"student_id":"AB1002",
"student_name":[
"Joe"]
},
{
"student_id":"AB1003",
"student_name":[
"Lorem"]
}]
}}
What will be the simple way to read student_id, student_name using PHP?
Use $obj = json_decode($yourJSONString); to convert it to an object.
Then use foreach($obj->response->docs as $doc) to iterate over the "docs".
You can then access the fields using $doc->student_id and $doc->student_name[0].
PHP has a json_decode function that will allow you to turn a JSON string into an array:
$array = json_decode($json_string, true);
$student_id = $array['response']['docs'][0]['student_id'];
...
Of course, you might want to iterate through the list of students instead of accessing index 0.
$result = json_decode($result, true);
$result['response']['docs'][0]['student_id'] ...
Why not just use one of the PHP clients for Solr, or the PHP response writer? See http://wiki.apache.org/solr/SolPHP
$json_a = json_decode($string, TRUE);
$json_o = json_decode($string);
#array method
foreach($json_a['response']['docs'] as $students)
{
echo $students['student_id']." name is ".$students['student_name'][0];
echo "<br>";
}
#Object Method`enter code here`
foreach($json_o->response->docs as $sthudent_o)
{
echo $sthudent_o->student_id. " name is ".$sthudent_o->student_name[0];
echo "<br>";
}

Categories