Json php decoding issue [duplicate] - php

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

Related

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

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

How to access object data inside an array and object after json_encode executed? [duplicate]

This question already has answers here:
Get value from JSON array in PHP
(4 answers)
Closed 6 years ago.
I have a json data from API and i want to insert them to my database table. I have extract $data using json_encode function, but when i tried to access data inside $myJson with some of this code, it gives me an error result.
$data = '{"posts":[{"post":{"math_score":"85","history_score":"70"}}]}';
$myJson = json_decode($data);
foreach ($myJson as $mj){
echo $mj['math_score'];
// echo $mj->math_score; <= error
// echo $mj[0]->post->math_score; <= error
// echo $mj->post->math_score; <= error
}
The error : Invalid argument supplied for foreach().
sorry for my bad grammar, any answer will be greatly appreciated. Thanks
There is built in php function json_decode()
Try
$json = '{"posts":[{"post":{"math_score":"85","history_score":"70"}}]}';
$json = json_decode($json);
echo $json->posts{0}->post->math_score;
By default json_decode return object. If you want an array then you need to pass second argument true to json_decode.
Try it with Array
$json = '{"posts":[{"post":{"math_score":"85","history_score":"70"}}]}';
$json = json_decode($json, true);
foreach ($json['posts'] as $mj)
{
echo $mj['post']['math_score'];
}

Want to get one value from json result using php [duplicate]

This question already has answers here:
Get data from JSON file with PHP [duplicate]
(3 answers)
Closed 7 years ago.
How can i get ID value only?
like so here: $fo = "11890408";
{"success":true,"result":{"success":[{"id":"11890408","device_id":"17183","message":"okey","status":"pending","send_at":1455046054,"queued_at":0,"sent_at":0,"delivered_at":0,"expires_at":1455049654,"canceled_at":0,"failed_at":0,"received_at":0,"error":"","created_at":1455046054,"contact":{"id":"2522330","name":"923336458112","number":"923336458112"}}],"fails":[]}}
Convert the json string to a php data structure and then navigate through the data tree of objects and arrays:
$json = '{
"success":true,
"result":{
"success[
{
"id":"11890408",
"device_id":"17183",
"message":"okey",
"status":"pending",
"send_at":1455046054,
"queued_at":0,
"sent_at":0,
"delivered_at":0,
"expires_at":1455049654,
"canceled_at":0,
"failed_at":0,
"received_at":0,
"error":"",
"created_at":1455046054,
"contact":{
"id":"2522330",
"name":"923336458112",
"number":"923336458112"
}
}
],
"fails":[]
}
}';
$data = json_decode($json);
$fo = $data->result->success[0]->id;

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

How do I parse JSON like geo:point in PHP? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to access an object property with a minus-sign?
I am trying to parse geo:point / geo:lat but getting nowhere fast. Here's the code I have so far
$content = get_data('http://ws.audioscrobbler.com/2.0/?method=geo.getevents&location=united+kingdom&api_key=XXX&format=json&limit=10000&festivalsonly=1');
$LFMdata = json_decode($content);
foreach ($LFMdata->events->event as $event) {
$venue_lat = $event->venue->location["geo:point"]["geo:lat"];
$venue_long = $event->venue->location["geo:point"]["geo:long"];
The JSON will contain something like
"geo:point": {
"geo:lat": "52.7352",
"geo:long": "-1.695392"
}
Anyone got any idea? Seen examples in JavaScript but not PHP
You can use
$venue_lat = $event->venue->location->{"geo:point"}->{"geo:lat"};
Or make it return everything like an associative array (second param of json_decode set to true):
$LFMdata = json_decode($content, true);
foreach ($LFMdata["events"]["event"] as $event) {
$venue_lat = $event["venue"]["location"]["geo:point"]["geo:lat"];
$venue_long = $event["venue"]["location"]["geo:point"]["geo:long"];

Categories