简体   繁体   中英

print_r in PHP returning “stdClass Object” on screen after using json_decode

<?php
$content = file_get_contents("https://min-api.cryptocompare.com/data/price?fsym=BTC&tsyms=USD");
$result = json_decode($content);
print_r ( $result);
?>

So I created that bit of code, but when I go to test it it returns " stdClass Object ( [USD] => 7531.74 )" on the screen. All I want is the 7531.74, how do I strip away the rest of it?

You can do one of two things:

// as a stdClass Object
$temp = json_decode($content);
$result = $temp->USD ;

Or, use an array:

// as an associative Array
$temp = json_decode($content,true);
$result = $temp['USD'] ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM