[英]json_decode foreach into table
嵌套數組有問題
我想將數組標題放入表的第一個單元格,但有一個問題明確說明了嵌套的foreach。
$content = file_get_contents("https://whattomine.com/asic.json");
$data = json_decode($content);
echo "coins";
foreach()
{
echo "<tr>";
echo "<th>".$coins->coinname."</th>";
echo "<th>".$coins->PoW."</th>";
echo "<th>".$coins->PoS."</th>";
echo "<th>".$coins->height."</th>";
echo "<th>".$coins->diff."</th>";
echo "<th>".$coins->supply."</th>";
echo "</tr>";
}
沒有您提供的密鑰,但還有下一個密鑰:
項目示例:
[LitecoinCash] => [
[id] => 231
[tag] => LCC
[algorithm] => SHA-256
[block_time] => 156.0
[block_reward] => 250
[block_reward24] => 250
[last_block] => 1460137
[difficulty] => 470292855.107
[difficulty24] => 396924068.28117
[nethash] => 12948028411711743
[exchange_rate] => 4.1E-6
[exchange_rate24] => 2.9135097493036E-6
[exchange_rate_vol] => 0.34130528
[exchange_rate_curr] => BTC
[market_cap] => $18,382,732.11
[estimated_rewards] => 149.54924
[estimated_rewards24] => 177.15706
[btc_revenue] => 0.00061315
[btc_revenue24] => 0.00072634
[profitability] => 102
[profitability24] => 121
[lagging] =>
[timestamp] => 1533048969
]
您需要哪一個?
我查看了數據,認為自己找到了想要的。
但是您嘗試獲取不在數據集中的值。
該json的結構為:
{"coins":
{"LitecoinCash":
{"id":231,"tag":"LCC",...}
},
...
}
所以這是獲取數據的方法:
$data = json_decode($content);
#var_dump($data);
echo "<table>";
foreach($data->coins as $title => $coin)
{
echo $title;
echo "<tr>";
echo "<th>".$title."</th>"; // the title as first cell
echo "<th>".$coin->id."</th>"; // added by me, coinname does not exist
echo "<th>".$coin->tag."</th>"; // added by me, PoW does not exist
#echo "<th>".$coin->PoS."</th>"; // all the others do not exist in the dataset.
#echo "<th>".$coin->height."</th>";
#echo "<th>".$coin->diff."</th>";
#echo "<th>".$coin->supply."</th>";
echo "</tr>";
}
echo "</table>";
// output:
// LitecoinCash 231 LCC
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.