首页 > 解决方案 > 使用 json_decode 后 PHP 中的 print_r 在屏幕上返回“stdClass Object”

问题描述

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

所以我创建了那段代码,但是当我去测试它时,它会在屏幕上返回“stdClass Object ([USD] => 7531.74)”。我想要的只是 7531.74,我该如何去掉它的其余部分?

标签: phpapi

解决方案


您可以执行以下两项操作之一:

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

或者,使用数组:

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

推荐阅读