Access Fields of PHP Object [duplicate] - php

This question already has answers here:
json_decode to array
(12 answers)
Closed 2 years ago.
I am trying to access the fields of a JSON-File. The JSON look like:
{"ip":"83.215.135.170","location:"{"country":"AT","region":"Salzburg","city":"Salzburg","lat":47.8125,"lng":13.0504,"postalCode":"5020"
I want to access the latitude and longitude of this file. My current code looks like that:
$data = json_decode($json);
echo $data["location:"]["lat"];
I get the error that the index is undefined. Can anybody help me?

You have a corupted JSON...here is the correct one
$json = '$json = '{"ip":"83.215.135.170","location":{"country":"AT","region":"Salzburg","city":"Salzburg","lat":47.8125,"lng":13.0504,"postalCode":"5020"}}';
With correct JSON you do this:
$decodedJson = json_decode($json,true);
And now you can do this:
echo $decodedJson["location"]["lat"]; // Prints 47.8125

if you want to access the fields like an array, you got to add "true" as a second parameter.
https://www.php.net/manual/de/function.json-decode.php
$data = json_decode($json, true);
echo $data["location:"]["lat"];

Related

how to get value from array [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
how to get value from array in php from api
here is my code
$data = callBitAPI('GET','https://min-api.cryptocompare.com/data/pricemultifull?fsyms='.strtoupper($bc->slug).'&tsyms=USD');
if(!empty($data)){
$data = json_decode($data,true);
// echo '<pre>';
// print_r($data);
$data = getRelevantCryptoArray($data,$bc->slug);
}
and here is my result
Please see Attachment
http://prntscr.com/k7hawi
Um... simply
$price = $data['RAW']['BTC']['USD']['PRICE'];
print_r($price);
unless I'm missing something?
dd($data['RAW']['BTC']['USD']['PRICE']) and see the result

How to access data from {string} [duplicate]

This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 5 years ago.
Some fields save data in the following way in one of my database field:
{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}
(* Don't know how it's called so i wrote string in the title)
How can i access data in particular for every "value".
One way could be to explode for every field but is there a better way to do this?
$ask_for_price_variable = [value from field];
if ($ask_for_price_variable == 'YES') {
// Do something
}
EDIT: As i said i did not know how it was called "JSON" so i could not search for it. Thank you all for the answers.
It is json data. You can access it using json_decode in php
$json = '{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}';
$data = json_decode($json,true);
$ask_for_price_variable = $data['ask_for_price_en-GB'];
Use json_decode
$str = '{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}';
print_r(json_decode($str, true));
You need to study about JSON type and how to encode or decode to get the data.
try this
echo "<pre>";
print_r( json_decode('{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}'));
to view the result , click run or hit f9, here
try json_decode function
$str = '{"per_meter_en-GB":"TEST_FOR_TEST","roll_40_en-GB":"","ask_for_price_en-GB":"YES"}';
$data = json_decode($str, true);
print_r($data);
output:
Array ( [per_meter_en-GB] => TEST_FOR_TEST [roll_40_en-GB] => [ask_for_price_en-GB] => YES )
here you can access required value from $data array eg. echo $data['per_meter_en-GB']; will output TEST_FOR_TEST

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

PHP: Get value from array? [duplicate]

This question already has answers here:
json_decode to array
(12 answers)
Closed 6 years ago.
I have a PHP variable that when I echo it will display an array that looks like this:
My Variable:
echo $mayVariable;
displays:
{"data":[{"id":"4756756575","name":"David","url":"https:\/\/www.somesite.com"}],"page":false}
I need to get the value from id within that array.
So I tried this:
echo $mayVariable[0]['id'];
But this doesn't give me anything.
I also tried:
echo $mayVariable['data']['id'];
and still I don't get anything in the echo...
Could someone please advise on this issue?
This JSON is an object array after decode it generally.
$json = '{"data":[{"id":"4756756575","name":"David","url":"https:\/\/www.somesite.com"}],"page":false}';
$arr = json_decode($json);
echo $arr->data[0]->id;//4756756575
If you use true as second parameter then:
$arr = json_decode($json, true);
echo $arr['data'][0]['id'];//4756756575

convert array got as string from "file_get_contents()" back to array [duplicate]

This question already has answers here:
How to convert JSON string to array
(17 answers)
Closed 8 years ago.
When I try to get data from an API using
file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");
I got the result as a string. Is there any way to convert it back to array?
The string which I got is
string(202) "{"query":{"count":1,"created":"2014-03-11T13:00:31Z","lang":"en-US","results":{"rate":{"id":"USDINR","Name":"USD to INR","Rate":"60.99","Date":"3/11/2014","Time":"9:00am","Ask":"61.00","Bid":"60.98"}}}}"{"error":"","msg":""}
please help me....
In your request, you ask that the returned format be JSON-encoded (via the format=json parameter), so you can just decode the response into an array using json_decode:
$response = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");
$response = json_decode($response, true);
// Parse the array as you would any other
You have a JSON answer here so jo need to dekode that.
<?php
$s = file_get_contents("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22USDINR%22)&format=json&env=store://datatables.org/alltableswithkeys&callback=");$data =
$data = json_decode($s,true);
?>
and the $data variable will be an array.

Categories