Having trouble accessing value from JSON data in PHP [duplicate] - php

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 2 years ago.
I have to try to access the value of "text" which is 4.9 in the JSON data below in PHP.
$json = '{"destination_addresses" : [ "Ibadan-Abeokuta Rd, Mokola, Ibadan, Nigeria" ],
"origin_addresses" : [ "Ogunwale Crescent, Ibadan, Nigeria" ],
"rows" : [
{ "elements" : [
{ "distance" :
{ "text" : "4.9 mi", "value" : 7819 },
"duration" : { "text" : "18 mins", "value" : 1067 },
"status" : "OK" }
]
}
]
, "status" : "OK" }';

Your JSON is a little bit complex. I have a solution below that access text property from the nested JSON.
$array = json_decode( $json, true );
echo ($array['rows'][0] ["elements"][0]['distance']['text']);

Related

Key Exist in a complex php variable [duplicate]

This question already has answers here:
How can I check if an array element exists?
(8 answers)
Closed 4 years ago.
How do I make sure that a key exists in a complex PHP variable?
Following is the JSON output of a Google API:
{
"destination_addresses" : [
"India"
],
"origin_addresses" : [
"India"
],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "86 m",
"value" : 86
},
"duration" : {
"text" : "1 min",
"value" : 24
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
sometimes it changes to this:
{
"destination_addresses" : [ "20.348326,85.8160893" ],
"origin_addresses" : [ "20.3487083,-85.8157674" ],
"rows" : [
{
"elements" : [
{
"status" : "ZERO_RESULTS"
}
]
}
],
"status" : "OK"
}
I have converted this JSON into a PHP variable using
$response_a = json_decode($response, true);
Now, how do I make sure that I read the key distance only when it is there in the output e.g:
$dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
in case of response 1; otherwise
$response_a['rows'][0]['elements'][0]['status']
You could use isset(...) eg:
if (isset($response_a['rows'][0]['elements'][0]['distance']['text'])) {
$dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
} else {
$dist =1;
} ;

Parse JSONArray in Jquery

I am Creating a webapp where i could get the distance and time of travel using google map API..
https://maps.googleapis.com/maps/api/distancematrix/json?origins=10.964,76.007&destinations=10.982,75.999
I am using HTTP method as given by google..
Its returning JSON Format as give below
{
"destination_addresses" : [ "Kanyakumari - Panvel Highway, Palathara, Kerala 676501, India" ],
"origin_addresses" : [ "Kanyakumari - Panvel Highway, Randathani, Kerala 676510, India" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "2.5 km",
"value" : 2526
},
"duration" : {
"text" : "4 mins",
"value" : 228
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
How could i extract the distance text and duration text using jquery and alert it..
Use file_get_contents() and json_decode() the output..
$contents = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?origins=10.964,76.007&destinations=10.982,75.999');
$data = json_decode($contents,true);
echo 'Distance : '.$data['rows'][0]['elements'][0]['distance']['text'];
echo "<br>";
echo 'Duration : '.$data['rows'][0]['elements'][0]['duration']['text'];
This will give you :
Distance : 2.5 km
Duration : 4 mins

Get specific value from JSON response [duplicate]

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 this response from Google Maps Distance Matrix API:
{
"destination_addresses" : [ "334-350 Hicks St, Brooklyn, NY 11201, USA" ],
"origin_addresses" : [ "565-569 Vermont St, Brooklyn, NY 11207, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "6.5 mi",
"value" : 10410
},
"duration" : {
"text" : "34 mins",
"value" : 2045
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
How to exactly get the "6.5 mi" value?
I tried this but doesn't work:
$distance = $response_a['rows'][0]['elements'][0]['distance']['text'];
You received a JSON string from Google API. Decode it before using.
$arr = json_decode($response_a, true);
echo $arr['rows'][0]['elements'][0]['distance']['text'];

De-code JSON data with PHP [duplicate]

This question already has answers here:
Access JSON object name in PHP
(4 answers)
Closed 7 years ago.
I have this PHP code:
<?php
$json = file_get_contents('https://maps.googleapis.com/maps/api/distancematrix/json?origins=1707%20Sixes%20Rd%20,%20Prince%20Frederick%20,%20MD,%2020678&destinations=2000%20Medical%20Pkwy%20,%20Annapolis%20,%20MD,%2021401&mode=driving&units=imperial');
$details = json_decode($json, TRUE);
?>
JSON data is as below, stored in $details
{
"destination_addresses" : [ "Annapolis, MD 21401, USA" ],
"origin_addresses" : [ "Prince Frederick, MD 20678, USA" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "37.8 mi",
"value" : 60775
},
"duration" : {
"text" : "51 mins",
"value" : 3039
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
I want to extract "37.8 mi" from it. Can anybody help?
Try this:
$array = json_decode($details, true);
$distance = $array['rows'][0]['elements'][0]['distance']['text'];

How to extract a value from this JSON response

I am using the file_get_contents() function in PHP to retrieve the contents on a page containing a JSON response. However, I am struggling to get the "text" data.
$response = file_get_contents(someData)
I can use var_dump(json_decode($response)); to show the data, however, I am trying to get the data from the "text" field only within duration.
So far I have tried
$response[0];
$response->rows[0]->elements[0]->duration[0]->text;
But I cannot seem to get the data. I have pasted the response below
{
"destination_addresses" : [ "Manchester, UK", "Liverpool, Merseyside, UK" ],
"origin_addresses" : [ "London, UK" ],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "335 km",
"value" : 335444
},
"duration" : {
"text" : "3 hours 36 mins",
"value" : 12955
},
"status" : "OK"
},
{
"distance" : {
"text" : "354 km",
"value" : 354415
},
"duration" : {
"text" : "3 hours 43 mins",
"value" : 13387
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
$response = json_decode(file_get_contents(someData));
$text = $response->rows[0]->elements[0]->duration->text;
var_dump($text);
Your only mistake was a [0] after duration.

Categories