无法解码 JSON 以 foreach 需要帮助
<?php
$workers = 'https://api.ethermine.org/miner/***/workers';
$workersContent = file_get_contents($workers);
$workersJson = json_decode($workersContent, true);
foreach ($workersJson['data']['worker'] as $worker) {
$output = $worker['worker'][1];
echo $output;
}
?>
如何获得工作线程阵列?找不到我的错误我是新手 ☺
访问网址时,我可以看到结构是这样的:
{"status":"OK","data":[{"worker":"gtx1050tix4","time":1520599200,"lastSeen":1520599074,"reportedHashrate":45518082,"currentHashrate":43333333.333333336,"validShares":39,"invalidShares":0,"staleShares":0,"averageHashrate":43559413.580246896}]}
如果你想得到gtx1050tix4
那么答案是
foreach ($workersJson['data']['worker'] as $worker) {
$output = $worker;
echo $output;
}
如果你想从那里获得整个数据和输出,答案是
foreach ($workersJson['data'] as $data) {
$worker = $data['worker'];
$time = $data['time']; // any key from json
echo $worker;
}
希望这能为您清除问题
请试试这个:
<?php
$workers = 'https://api.ethermine.org/miner/YOURAPIKEY/workers';
$workersContent = file_get_contents($workers);
$workersJson = json_decode($workersContent, true);
//print_r($workersJson);
foreach ($workersJson['data'] as $worker) {
$output = $worker['worker'];
echo $output;
}
?>
你可以执行以下操作:
$workers = 'https://api.ethermine.org/miner/YOURAPIKEY/workers';
$workersContent = file_get_contents($workers);
$workersJson = json_decode($workersContent, true);
foreach ($workersJson['data'] as $worker) {
//$worker["worker"] <-- accessing gtx1050tix4
//$worker["time"] <-- accessing 1520599200
//etc
echo "<pre>";
print_r( $worker );
echo "</pre>";
}
这将导致:
Array
(
[worker] => gtx1050tix4
[time] => 1520599200
[lastSeen] => 1520599074
[reportedHashrate] => 45518082
[currentHashrate] => 43333333.333333
[validShares] => 39
[invalidShares] => 0
[staleShares] => 0
[averageHashrate] => 43559413.580247
)