Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从json响应laravel中获取所选值?_Php_Laravel - Fatal编程技术网

Php 如何从json响应laravel中获取所选值?

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

我想使用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 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'];