如何在php(coinmarket api)中使用foreach打印以下json数据
我试图在php的foreach循环中打印以下json数据。谁能告诉我怎么做?我找了很多,但都没找到。 我从coinmarket api获取此数据,并使用以下方法打印此代码: 但未能分别获取每个值如何在php(coinmarket api)中使用foreach打印以下json数据,php,json,api,Php,Json,Api,我试图在php的foreach循环中打印以下json数据。谁能告诉我怎么做?我找了很多,但都没找到。 我从coinmarket api获取此数据,并使用以下方法打印此代码: 但未能分别获取每个值 $curl = curl_init(); // Get cURL resource // Set cURL options curl_setopt_array($curl, array( CURLOPT_URL => $request, // se
$curl = curl_init(); // Get cURL resource
// Set cURL options
curl_setopt_array($curl, array(
CURLOPT_URL => $request, // set the request URL
CURLOPT_HTTPHEADER => $headers, // set the headers
CURLOPT_RETURNTRANSFER => 1 // ask for raw response instead of bool
));
$response = curl_exec($curl); // Send the request, save the response
print_r(json_decode($response));
下面是我想在foreach循环中使用的代码:
stdClass Object ( [status] => stdClass Object ( [timestamp] => 2019-10-09T03:13:30.051Z [error_code] => 0 [error_message] => [elapsed] => 9 [credit_count] => 1 [notice] => ) [data] => stdClass Object ( [BTC] => stdClass Object ( [id] => 1 [name] => Bitcoin [symbol] => BTC [slug] => bitcoin [num_market_pairs] => 7935 [date_added] => 2013-04-28T00:00:00.000Z [tags] => Array ( [0] => mineable ) [max_supply] => 21000000 [circulating_supply] => 17981825 [total_supply] => 17981825 [platform] => [cmc_rank] => 1 [last_updated] => 2019-10-09T03:12:33.000Z [quote] => stdClass Object ( [USD] => stdClass Object ( [price] => 8195.0290967 [volume_24h] => 14981665979.667 [percent_change_1h] => -0.224992 [percent_change_24h] => -1.35388 [percent_change_7d] => -1.38135 [market_cap] => 147361579086.77 [last_updated] => 2019-10-09T03:12:33.000Z ) ) ) ) )
请格式化您的问题、代码和输出,以便可读。你试过什么了?你需要什么数据 要获取在数据中检索到的所有硬币的名称列表,您可以使用以下代码:
$response = json_decode($response);
foreach ($response->data as $coin){ // assuming the data could contain multiple coins
echo $coin->name;
echo '<br>';
}
$response=json\u decode($response);
foreach($response->data as$coin){//假设数据可以包含多个硬币
echo$coin->name;
回声“
”;
}
提示:如果您想在HTML页面中看到print_r()的良好格式输出,可以将其放在PRE标记之间
echo '<pre>';
print_r($my_object);
echo '</pre>';
echo';
打印(我的对象);
回声';