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
Related
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 1 year ago.
I've a json response like this:
'{"success":true,"code":200,"message":"Success","data":[{"title":"title1","productKey":"123","thumbnailUrl":"https://example.com/1.jpg","star":0},{"title":"title2","productKey":"234","thumbnailUrl":"https://example.com/2.jpg","star":0}]}'
above code is in a variable as string
I need just product keys: 123,234;
how can I get product keys from this reponse?
Thanks
Use json_decode to turn it into an associative array.
$jsonString = '{"success":true,"code":200,"message":"Success","data":[{"title":"title1","productKey":"123","thumbnailUrl":"https://example.com/1.jpg","star":0},{"title":"title2","productKey":"234","thumbnailUrl":"https://example.com/2.jpg","star":0}]}';
$json = json_decode($jsonString, true);
$productKeys = [];
foreach($json["data"] as $val)
{
$productKeys[] = $val["productKey"];
}
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"];
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 a response from my code which looks like this
Controller
$results = $response
echo $result->item;
exit;
Response
{"item":"Samsung A","location":"Hamburg Avenue 5920"}
I want to get only item from the response..How do i achieve this ? The response is json encoded
You need to convert json to object, and after that you can get the element:
$respObject = json_decode($response);
echo $respObject->item;
exit;
json_decode function will helps.
$json_data = '{"item":"Samsung A","location":"Hamburg Avenue 5920"}';
$result = json_decode($json_data);
echo $result->item;
Use json_decode the result will converted into array
$results = json_decode($response);
print_r( $results);
exit;
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 4 years ago.
I want get Json objects from the json array and store each values in mysql using php.
$arr = "[{"fruit":"Apples","isSelected":false,"number":0},{"fruit":"Oranges","isSelected":false,"number":0},{"fruit":"Potatoes","isSelected":false,"number":0},{"fruit":"Tomatoes","isSelected":false,"number":0},{"fruit":"Grapes","isSelected":false,"number":0},{"fruit":"Dhanana","isSelected":false,"number":0}]";
try this code
Using json_decode
<?php
$arr = '[{"fruit":"Apples","isSelected":false,"number":0},{"fruit":"Oranges","isSelected":false,"number":0},{"fruit":"Potatoes","isSelected":false,"number":0},{"fruit":"Tomatoes","isSelected":false,"number":0},{"fruit":"Grapes","isSelected":false,"number":0},{"fruit":"Dhanana","isSelected":false,"number":0}]';
$data = json_decode($arr);
echo "<pre>";
print_r($data);
foreach($data as $k=>$v){
echo $v->fruit." ".$v->isSelected." ".$v->number."<br>"; //store in db here using value
print_r($v);
}
?>
This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 11 months ago.
i got this json encoded data being sent to me.
can u tell me how to get each of the individual elements ?
Something like:
$ticket
$customer
$user
{"ticket":{"id":"10909446","number":"152"},"customer":{"id":"3909381","fname":"","lname":"","email":"me#site.com","emails":["me#site.com"]},"user":{"fname":"Test","lname":"Me","id":17396,"role":"admin"}}
this is a basic view on how my code runs.
$ret = array('html' => '');
$data = json_decode($data , true);
$ret['html'] = '<ul><li>'.$data->ticket->number.'</li></ul>';
echo json_encode($ret);
exit;
it only prints the circle from the li tags.
To clarify #Cthulhu's answer :
$test = '{"ticket":{"id":"10909446","number":"152"},"customer":{"id":"3909381","fname":"","lname":"","email":"me#site.com","emails":["me#site.com"]},"user":{"fname":"Test","lname":"Me","id":17396,"role":"admin"}}';
$data = json_decode($test);
echo $data->ticket->id;
outputs
10909446
json_decode make the JSON into a stdClass object, and then you can access the values as that.
$data = json_decode($test);
$ret = array();
$ret['html']='<ul><li>'.$data->ticket->number.'</li></ul>';
return
json_encode($ret);
will return
{"html":"<ul><li>152<\/li><\/ul>"}
json_decode is the answer for you.