Php 如何从json响应laravel中获取所选值?
我想使用coinmarketcap的API将btc的价格转换为美元Php 如何从json响应laravel中获取所选值?,php,laravel,Php,Laravel,我想使用coinmarketcap的API将btc的价格转换为美元 $response = curl_exec($curl); // Send the request, save the response $type = json_decode($response,true); 获得200 Ok响应 我想从美元中获得价格价值,但在我尝试时它不起作用 因为你正在使用 $response = curl_exec($curl); // Send the request, save the respo
$response = curl_exec($curl); // Send the request, save the response
$type = json_decode($response,true);
获得200 Ok响应
我想从美元中获得价格价值,但在我尝试时它不起作用
因为你正在使用
$response = curl_exec($curl); // Send the request, save the response
$type = json_decode($response,true);
这意味着您的对象将转换为关联数组,
要访问价格使用:
$type['data']['quote']['USD']['price'];
你该走了
编辑
如果$type
包含多个元素,则需要循环它们
foreach($type['data'] as $singleQuote){
$price = $singleQuote['quote']['USD']['price'];
echo 'Quote for: '.$singleQuote['symbol'].' in USD: '.$price.'<br/>';
}
foreach($type['data']作为$singleQuote){
$price=$singleQuote['quote']['USD']['price'];
echo“报价为:”.$singleQuote['symbol']”美元:“.$price.”
;
}
因为您正在传递将其转换为关联数组的第二个参数true
:
$response = curl_exec($curl); // Send the request, save the response
$type = json_decode($response,true);
$examount = $type['data']['quote']['USD']['price'];
我希望以上将根据coinmarketcap()的文档解决您的问题。问题是由$request返回null引起的。以上所有内容都是正确的。尝试此
$examount=$type['USD']->price代码>@kerbholz先生,请你解释一下我用了很多不同的方法,但没有worked@Rezash我想你已经错过了json中的一个级别,试着在你的代码中加入quote
,我的意思是这样的$examount=$type->quote->USD->price代码>@Rezash尝试获取非对象的属性响应“尝试获取非对象的属性”后的行获取错误
foreach($type['data'] as $singleQuote){
$price = $singleQuote['quote']['USD']['price'];
echo 'Quote for: '.$singleQuote['symbol'].' in USD: '.$price.'<br/>';
}
$response = curl_exec($curl); // Send the request, save the response
$type = json_decode($response,true);
$examount = $type['data']['quote']['USD']['price'];